diff --git a/core/java/com/android/internal/os/LongArrayMultiStateCounter.java b/core/java/com/android/internal/os/LongArrayMultiStateCounter.java index 47a95c0ae198d..aecab3db4cc49 100644 --- a/core/java/com/android/internal/os/LongArrayMultiStateCounter.java +++ b/core/java/com/android/internal/os/LongArrayMultiStateCounter.java @@ -133,6 +133,14 @@ public final class LongArrayMultiStateCounter implements Parcelable { mLength = native_getArrayLength(mNativeObject); } + /** + * Enables or disables the counter. When the counter is disabled, it does not + * accumulate counts supplied by the {@link #updateValues} method. + */ + public void setEnabled(boolean enabled, long timestampMs) { + native_setEnabled(mNativeObject, enabled, timestampMs); + } + /** * Sets the current state to the supplied value. */ @@ -158,6 +166,13 @@ public final class LongArrayMultiStateCounter implements Parcelable { native_updateValues(mNativeObject, longArrayContainer.mNativeObject, timestampMs); } + /** + * Resets the accumulated counts to 0. + */ + public void reset() { + native_reset(mNativeObject); + } + /** * Populates longArrayContainer with the accumulated counts for the specified state. */ @@ -204,6 +219,10 @@ public final class LongArrayMultiStateCounter implements Parcelable { @CriticalNative private static native long native_getReleaseFunc(); + @CriticalNative + private static native void native_setEnabled(long nativeObject, boolean enabled, + long timestampMs); + @CriticalNative private static native void native_setState(long nativeObject, int state, long timestampMs); @@ -211,6 +230,9 @@ public final class LongArrayMultiStateCounter implements Parcelable { private static native void native_updateValues(long nativeObject, long longArrayContainerNativeObject, long timestampMs); + @CriticalNative + private static native void native_reset(long nativeObject); + @CriticalNative private static native void native_getCounts(long nativeObject, long longArrayContainerNativeObject, int state); diff --git a/core/jni/com_android_internal_os_LongArrayMultiStateCounter.cpp b/core/jni/com_android_internal_os_LongArrayMultiStateCounter.cpp index 716c85c2b0fea..8326de22e9387 100644 --- a/core/jni/com_android_internal_os_LongArrayMultiStateCounter.cpp +++ b/core/jni/com_android_internal_os_LongArrayMultiStateCounter.cpp @@ -43,6 +43,12 @@ static jlong native_getReleaseFunc() { return reinterpret_cast(native_dispose); } +static void native_setEnabled(jlong nativePtr, jboolean enabled, jlong timestamp) { + battery::LongArrayMultiStateCounter *counter = + reinterpret_cast(nativePtr); + counter->setEnabled(enabled, timestamp); +} + static void native_setState(jlong nativePtr, jint state, jlong timestamp) { battery::LongArrayMultiStateCounter *counter = reinterpret_cast(nativePtr); @@ -59,6 +65,12 @@ static void native_updateValues(jlong nativePtr, jlong longArrayContainerNativeP counter->updateValue(*vector, timestamp); } +static void native_reset(jlong nativePtr) { + battery::LongArrayMultiStateCounter *counter = + reinterpret_cast(nativePtr); + counter->reset(); +} + static void native_getCounts(jlong nativePtr, jlong longArrayContainerNativePtr, jint state) { battery::LongArrayMultiStateCounter *counter = reinterpret_cast(nativePtr); @@ -166,10 +178,14 @@ static const JNINativeMethod g_LongArrayMultiStateCounter_methods[] = { // @CriticalNative {"native_getReleaseFunc", "()J", (void *)native_getReleaseFunc}, // @CriticalNative + {"native_setEnabled", "(JZJ)V", (void *)native_setEnabled}, + // @CriticalNative {"native_setState", "(JIJ)V", (void *)native_setState}, // @CriticalNative {"native_updateValues", "(JJJ)V", (void *)native_updateValues}, // @CriticalNative + {"native_reset", "(J)V", (void *)native_reset}, + // @CriticalNative {"native_getCounts", "(JJI)V", (void *)native_getCounts}, // @FastNative {"native_toString", "(J)Ljava/lang/String;", (void *)native_toString}, diff --git a/core/tests/coretests/src/com/android/internal/os/LongArrayMultiStateCounterTest.java b/core/tests/coretests/src/com/android/internal/os/LongArrayMultiStateCounterTest.java index da158e645de6c..9eb4ccb2663bb 100644 --- a/core/tests/coretests/src/com/android/internal/os/LongArrayMultiStateCounterTest.java +++ b/core/tests/coretests/src/com/android/internal/os/LongArrayMultiStateCounterTest.java @@ -47,6 +47,53 @@ public class LongArrayMultiStateCounterTest { "[0: {75, 150, 225, 300}, 1: {25, 50, 75, 100}] updated: 9000 currentState: 0"); } + @Test + public void setEnabled() { + LongArrayMultiStateCounter counter = new LongArrayMultiStateCounter(2, 4); + counter.setState(0, 1000); + updateValue(counter, new long[]{0, 0, 0, 0}, 1000); + updateValue(counter, new long[]{100, 200, 300, 400}, 2000); + + assertCounts(counter, 0, new long[]{100, 200, 300, 400}); + + counter.setEnabled(false, 3000); + + // Partially included, because the counter is disabled after the previous update + updateValue(counter, new long[]{200, 300, 400, 500}, 4000); + + // Count only 50%, because the counter was disabled for 50% of the time + assertCounts(counter, 0, new long[]{150, 250, 350, 450}); + + // Not counted because the counter is disabled + updateValue(counter, new long[]{250, 350, 450, 550}, 5000); + + counter.setEnabled(true, 6000); + + updateValue(counter, new long[]{300, 400, 500, 600}, 7000); + + // Again, take 50% of the delta + assertCounts(counter, 0, new long[]{175, 275, 375, 475}); + } + + @Test + public void reset() { + LongArrayMultiStateCounter counter = new LongArrayMultiStateCounter(2, 4); + counter.setState(0, 1000); + updateValue(counter, new long[]{0, 0, 0, 0}, 1000); + updateValue(counter, new long[]{100, 200, 300, 400}, 2000); + + assertCounts(counter, 0, new long[]{100, 200, 300, 400}); + + counter.reset(); + + assertCounts(counter, 0, new long[]{0, 0, 0, 0}); + + updateValue(counter, new long[]{200, 300, 400, 500}, 3000); + updateValue(counter, new long[]{300, 400, 500, 600}, 4000); + + assertCounts(counter, 0, new long[]{100, 100, 100, 100}); + } + @Test public void parceling() { LongArrayMultiStateCounter counter = new LongArrayMultiStateCounter(2, 4);