BatteryDefender: Use "present" instead of "online" for wireless adapters

This adapts to the kernel change that changes the semantics of the
wireless nodes.

Test: atest HealthTestCases
Bug: 175614345
Change-Id: Ie3dd79d9608b7a47b5ad3fc4b73b57433b71aa70
(cherry picked from commit 1ce73fc4661d9aebf4f8daeaa4ce5aea4ec4b833)
diff --git a/health/BatteryDefender.cpp b/health/BatteryDefender.cpp
index 5873567..542caff 100644
--- a/health/BatteryDefender.cpp
+++ b/health/BatteryDefender.cpp
@@ -34,10 +34,12 @@
 namespace pixel {
 namespace health {
 
-BatteryDefender::BatteryDefender(const char *pathChargeLevelStart, const char *pathChargeLevelStop,
-                                 const int32_t timeToActivateSecs,
+BatteryDefender::BatteryDefender(const char *pathWirelessPresent, const char *pathChargeLevelStart,
+                                 const char *pathChargeLevelStop, const int32_t timeToActivateSecs,
                                  const int32_t timeToClearTimerSecs)
-    : kPathChargeLevelStart(pathChargeLevelStart),
+
+    : kPathWirelessPresent(pathWirelessPresent),
+      kPathChargeLevelStart(pathChargeLevelStart),
       kPathChargeLevelStop(pathChargeLevelStop),
       kTimeToActivateSecs(timeToActivateSecs),
       kTimeToClearTimerSecs(timeToClearTimerSecs) {
@@ -145,12 +147,14 @@
     }
 }
 
-bool BatteryDefender::isChargePowerAvailable(const bool chargerWirelessOnline) {
+bool BatteryDefender::isChargePowerAvailable(void) {
     // USB presence is an indicator of power availability
     const bool chargerPresentWired = readFileToInt(kPathUSBChargerPresent) != 0;
+    const bool chargerPresentWireless = readFileToInt(kPathWirelessPresent) != 0;
     mIsUsbPresent = chargerPresentWired;
+    mIsWirelessPresent = chargerPresentWireless;
 
-    return chargerPresentWired || chargerWirelessOnline;
+    return chargerPresentWired || chargerPresentWireless;
 }
 
 bool BatteryDefender::isDefaultChargeLevel(const int start, const int stop) {
@@ -334,7 +338,7 @@
     /**
      * If the kernel is forcing the input current limit to 0, then the online status may
      * need to be overwritten. Also, setting a charge limit below the current charge level
-     * may disable the adapter. This does not occur for wireless adapters.
+     * may disable the adapter.
      * Note; only override "online" if necessary (all "online"s are false).
      */
     if (props->chargerUsbOnline == false && props->chargerAcOnline == false) {
@@ -352,6 +356,13 @@
         mWasAcOnline = props->chargerAcOnline;
         mWasUsbOnline = props->chargerUsbOnline;
     }
+
+    /* Do the same as above for wireless adapters */
+    if (props->chargerWirelessOnline == false) {
+        if (mIsWirelessPresent && props->batteryHealth == android::BATTERY_HEALTH_OVERHEAT) {
+            props->chargerWirelessOnline = true;
+        }
+    }
 }
 
 void BatteryDefender::update(struct android::BatteryProperties *props) {
@@ -365,7 +376,7 @@
     const int chargeLevelVendorStop =
             android::base::GetIntProperty(kPropChargeLevelVendorStop, kChargeLevelDefaultStop);
     mIsDefenderDisabled = isBatteryDefenderDisabled(chargeLevelVendorStart, chargeLevelVendorStop);
-    mIsPowerAvailable = isChargePowerAvailable(props->chargerWirelessOnline);
+    mIsPowerAvailable = isChargePowerAvailable();
     mTimeBetweenUpdateCalls = getDeltaTimeSeconds(&mTimePreviousSecs);
 
     // Run state machine
diff --git a/health/include/pixelhealth/BatteryDefender.h b/health/include/pixelhealth/BatteryDefender.h
index bf3c492..7819147 100644
--- a/health/include/pixelhealth/BatteryDefender.h
+++ b/health/include/pixelhealth/BatteryDefender.h
@@ -44,7 +44,8 @@
 class BatteryDefender {
   public:
     // Set default google charger paths - can be overridden for other devices
-    BatteryDefender(const char *pathChargeLevelStart =
+    BatteryDefender(const char *pathWirelessPresent = "/sys/class/power_supply/wireless/present",
+                    const char *pathChargeLevelStart =
                             "/sys/devices/platform/soc/soc:google,charger/charge_start_level",
                     const char *pathChargeLevelStop =
                             "/sys/devices/platform/soc/soc:google,charger/charge_stop_level",
@@ -71,6 +72,7 @@
             [STATE_ACTIVE] = "ACTIVE",
     };
 
+    const char *const kPathWirelessPresent;
     const char *const kPathChargeLevelStart;
     const char *const kPathChargeLevelStop;
     const int32_t kTimeToActivateSecs;
@@ -114,6 +116,7 @@
     int64_t mTimeBetweenUpdateCalls = 0;
     int64_t mTimePreviousSecs;
     bool mIsUsbPresent = false;
+    bool mIsWirelessPresent = false;
     bool mIsPowerAvailable = false;
     bool mIsDefenderDisabled = false;
     int32_t mTimeToActivateSecsModified;
@@ -152,7 +155,7 @@
     bool writeIntToFile(const char *path, const int value);
     void writeTimeToFile(const char *path, const int value, int64_t *previous);
     void writeChargeLevelsToFile(const int vendorStart, const int vendorStop);
-    bool isChargePowerAvailable(const bool chargerWirelessOnline);
+    bool isChargePowerAvailable(void);
     bool isDefaultChargeLevel(const int start, const int stop);
     bool isBatteryDefenderDisabled(const int vendorStart, const int vendorStop);
     void addTimeToChargeTimers(void);
diff --git a/health/test/TestBatteryDefender.cpp b/health/test/TestBatteryDefender.cpp
index f050ccc..7366aab 100644
--- a/health/test/TestBatteryDefender.cpp
+++ b/health/test/TestBatteryDefender.cpp
@@ -134,6 +134,7 @@
 };
 
 const char *kPathWiredChargerPresent = "/sys/class/power_supply/usb/present";
+const char *kPathWirelessChargerPresent = "/sys/class/power_supply/wireless/present";
 const char *kPathPersistChargerPresentTime = "/mnt/vendor/persist/battery/defender_charger_time";
 const char *kPathPersistDefenderActiveTime = "/mnt/vendor/persist/battery/defender_active_time";
 const char *kPathStartLevel = "/sys/devices/platform/soc/soc:google,charger/charge_start_level";
@@ -165,8 +166,18 @@
             .WillByDefault(DoAll(SetArgPointee<1>(std::string("1")), Return(true)));
 }
 
+static void wirelessPresent(void) {
+    ON_CALL(*mock, ReadFileToString(kPathWirelessChargerPresent, _, _))
+            .WillByDefault(DoAll(SetArgPointee<1>(std::string("1")), Return(true)));
+}
+
+static void wirelessNotPresent(void) {
+    ON_CALL(*mock, ReadFileToString(kPathWirelessChargerPresent, _, _))
+            .WillByDefault(DoAll(SetArgPointee<1>(std::string("0")), Return(true)));
+}
+
 static void powerAvailable(void) {
-    props.chargerWirelessOnline = 1;
+    wirelessPresent();
     usbPresent();
 }
 
@@ -433,7 +444,7 @@
     InSequence s;
 
     // Power ON
-    props.chargerWirelessOnline = true;
+    wirelessPresent();
 
     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(1000), kPathPersistChargerPresentTime, _));
     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
@@ -446,7 +457,7 @@
     battDefender.update(&props);
 
     // Power OFF
-    props.chargerWirelessOnline = false;
+    wirelessNotPresent();
 
     // Maintain kPathPersistChargerPresentTime = 1060
     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
@@ -465,7 +476,7 @@
     battDefender.update(&props);
 
     // Power ON
-    props.chargerWirelessOnline = true;
+    wirelessPresent();
 
     // Maintain kPathPersistChargerPresentTime = 0
     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
@@ -501,7 +512,7 @@
     InSequence s;
 
     // Power ON
-    props.chargerWirelessOnline = true;
+    wirelessPresent();
 
     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(1000), kPathPersistChargerPresentTime, _));
     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "CONNECTED"));
@@ -514,7 +525,7 @@
     battDefender.update(&props);
 
     // Power OFF
-    props.chargerWirelessOnline = false;
+    wirelessNotPresent();
 
     EXPECT_CALL(*mock, WriteStringToFile(std::to_string(0), kPathPersistChargerPresentTime, _));
     EXPECT_CALL(*mock, SetProperty(kPropBatteryDefenderState, "DISCONNECTED"));