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

Commit b9346715 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Add LongArrayMultiStateCounter.reset() and setEnabled()

Bug: 197162116
Test: atest FrameworksCoreTests:LongArrayMultiStateCounterTest
Change-Id: I2f05c2ce56eaa52835fb350ecb0e5e4408f6b879
parent c1fa3a71
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -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);
+16 −0
Original line number Diff line number Diff line
@@ -43,6 +43,12 @@ static jlong native_getReleaseFunc() {
    return reinterpret_cast<jlong>(native_dispose);
}

static void native_setEnabled(jlong nativePtr, jboolean enabled, jlong timestamp) {
    battery::LongArrayMultiStateCounter *counter =
            reinterpret_cast<battery::LongArrayMultiStateCounter *>(nativePtr);
    counter->setEnabled(enabled, timestamp);
}

static void native_setState(jlong nativePtr, jint state, jlong timestamp) {
    battery::LongArrayMultiStateCounter *counter =
            reinterpret_cast<battery::LongArrayMultiStateCounter *>(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<battery::LongArrayMultiStateCounter *>(nativePtr);
    counter->reset();
}

static void native_getCounts(jlong nativePtr, jlong longArrayContainerNativePtr, jint state) {
    battery::LongArrayMultiStateCounter *counter =
            reinterpret_cast<battery::LongArrayMultiStateCounter *>(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},
+47 −0
Original line number Diff line number Diff line
@@ -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);