Add MultiStateCounter.addValue and make updateValue return delta

Bug: 197162116
Test: atest libbattery_test
Change-Id: I790ed0b805a88aa6ee9659f8494af8edf693d931
diff --git a/libs/battery/MultiStateCounter.h b/libs/battery/MultiStateCounter.h
index e1ee07c..112fb85 100644
--- a/libs/battery/MultiStateCounter.h
+++ b/libs/battery/MultiStateCounter.h
@@ -62,7 +62,13 @@
 
     void setValue(state_t state, const T& value);
 
-    void updateValue(const T& value, time_t timestamp);
+    /**
+     * Updates the value for the current state and returns the delta from the previously
+     * set value.
+     */
+    const T& updateValue(const T& value, time_t timestamp);
+
+    void addValue(const T& value);
 
     void reset();
 
@@ -161,7 +167,7 @@
 }
 
 template <class T>
-void MultiStateCounter<T>::updateValue(const T& value, time_t timestamp) {
+const T& MultiStateCounter<T>::updateValue(const T& value, time_t timestamp) {
     // If the counter is disabled, we ignore the update, except when the counter got disabled after
     // the previous update, in which case we still need to pick up the residual delta.
     if (isEnabled || lastUpdateTimestamp < lastStateChangeTimestamp) {
@@ -195,6 +201,16 @@
     }
     lastValue = value;
     lastUpdateTimestamp = timestamp;
+    return deltaValue;
+}
+
+template <class T>
+void MultiStateCounter<T>::addValue(const T& value) {
+    if (!isEnabled) {
+        return;
+    }
+
+    add(&states[currentState].counter, value, 1 /* numerator */, 1 /* denominator */);
 }
 
 template <class T>
@@ -242,7 +258,9 @@
     } else {
         str << " currentState: none";
     }
-
+    if (!isEnabled) {
+        str << " disabled";
+    }
     return str.str();
 }
 
diff --git a/libs/battery/MultiStateCounterTest.cpp b/libs/battery/MultiStateCounterTest.cpp
index 319ba76..848fd10 100644
--- a/libs/battery/MultiStateCounterTest.cpp
+++ b/libs/battery/MultiStateCounterTest.cpp
@@ -52,11 +52,12 @@
     DoubleMultiStateCounter testCounter(3, 0);
     testCounter.updateValue(0, 0);
     testCounter.setState(1, 0);
-    testCounter.updateValue(3.14, 3000);
+    double delta = testCounter.updateValue(3.14, 3000);
 
     EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
     EXPECT_DOUBLE_EQ(3.14, testCounter.getCount(1));
     EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
+    EXPECT_DOUBLE_EQ(3.14, delta);
 }
 
 TEST_F(MultiStateCounterTest, stateChange) {
@@ -177,12 +178,31 @@
 
     // Time moves back. The negative delta from 2000 to 1000 is ignored
     testCounter.updateValue(8.0, 1000);
-    testCounter.updateValue(11.0, 3000);
+    double delta = testCounter.updateValue(11.0, 3000);
 
     // The total accumulated count is:
     //  6.0          // For the period 0-2000
     //  +(11.0-8.0)  // For the period 1000-3000
     EXPECT_DOUBLE_EQ(9.0, testCounter.getCount(0));
+
+    //  11.0-8.0
+    EXPECT_DOUBLE_EQ(3.0, delta);
+}
+
+TEST_F(MultiStateCounterTest, addValue) {
+    DoubleMultiStateCounter testCounter(1, 0);
+    testCounter.updateValue(0, 0);
+    testCounter.setState(0, 0);
+    testCounter.updateValue(6.0, 2000);
+
+    testCounter.addValue(8.0);
+
+    EXPECT_DOUBLE_EQ(14.0, testCounter.getCount(0));
+
+    testCounter.setEnabled(false, 3000);
+    testCounter.addValue(888.0);
+
+    EXPECT_DOUBLE_EQ(14.0, testCounter.getCount(0));
 }
 
 TEST_F(MultiStateCounterTest, toString) {