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();
}