diff options
author | 2024-11-08 00:45:10 +0000 | |
---|---|---|
committer | 2024-11-08 00:45:44 +0000 | |
commit | 6266b5b1795fbf4a986dd01485f77120fee932a1 (patch) | |
tree | da93de0444c480f6e7819af9ab0e24b4a73db286 /ravenwood/runtime-helper-src | |
parent | 6cc7c9df3aa08ef16efe880fe71fc5c0bf4d21b8 (diff) |
[Ravenwood] Move redirection classes out of f/b/r
For all redirection classes that are not highly tied to Ravenwood's
internal implementation, they should live side-by-side with the original
code it is replacing.
Bug: 375620876
Flag: EXEMPT host test change only
Test: $ANDROID_BUILD_TOP/frameworks/base/ravenwood/scripts/run-ravenwood-tests.sh
Change-Id: Ia0742d0144e729b1beb8502fdf86978774a59100
Merged-In: I0d4f2c399e3ba10ff95d690a01fc0bbfb2f8d585
Diffstat (limited to 'ravenwood/runtime-helper-src')
5 files changed, 0 insertions, 972 deletions
diff --git a/ravenwood/runtime-helper-src/framework/android/database/CursorWindow_host.java b/ravenwood/runtime-helper-src/framework/android/database/CursorWindow_host.java deleted file mode 100644 index e21a9cd71a2d..000000000000 --- a/ravenwood/runtime-helper-src/framework/android/database/CursorWindow_host.java +++ /dev/null @@ -1,191 +0,0 @@ -/* - * Copyright (C) 2023 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package android.database; - -import android.database.sqlite.SQLiteException; -import android.os.Parcel; -import android.util.Base64; - -import java.text.DecimalFormat; -import java.text.ParsePosition; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; - -public class CursorWindow_host { - - private static final HashMap<Long, CursorWindow_host> sInstances = new HashMap<>(); - private static long sNextId = 1; - - private String mName; - private int mColumnNum; - private static class Row { - String[] mFields; - int[] mTypes; - } - - private final List<Row> mRows = new ArrayList<>(); - - public static long nativeCreate(String name, int cursorWindowSize) { - CursorWindow_host instance = new CursorWindow_host(); - instance.mName = name; - long instanceId = sNextId++; - sInstances.put(instanceId, instance); - return instanceId; - } - - public static void nativeDispose(long windowPtr) { - sInstances.remove(windowPtr); - } - - public static String nativeGetName(long windowPtr) { - return sInstances.get(windowPtr).mName; - } - - public static boolean nativeSetNumColumns(long windowPtr, int columnNum) { - sInstances.get(windowPtr).mColumnNum = columnNum; - return true; - } - - public static int nativeGetNumRows(long windowPtr) { - return sInstances.get(windowPtr).mRows.size(); - } - - public static boolean nativeAllocRow(long windowPtr) { - CursorWindow_host instance = sInstances.get(windowPtr); - Row row = new Row(); - row.mFields = new String[instance.mColumnNum]; - row.mTypes = new int[instance.mColumnNum]; - Arrays.fill(row.mTypes, Cursor.FIELD_TYPE_NULL); - instance.mRows.add(row); - return true; - } - - private static boolean put(long windowPtr, String value, int type, int row, int column) { - CursorWindow_host instance = sInstances.get(windowPtr); - if (row >= instance.mRows.size() || column >= instance.mColumnNum) { - return false; - } - Row r = instance.mRows.get(row); - r.mFields[column] = value; - r.mTypes[column] = type; - return true; - } - - public static int nativeGetType(long windowPtr, int row, int column) { - CursorWindow_host instance = sInstances.get(windowPtr); - if (row >= instance.mRows.size() || column >= instance.mColumnNum) { - return Cursor.FIELD_TYPE_NULL; - } - - return instance.mRows.get(row).mTypes[column]; - } - - public static boolean nativePutString(long windowPtr, String value, - int row, int column) { - return put(windowPtr, value, Cursor.FIELD_TYPE_STRING, row, column); - } - - public static String nativeGetString(long windowPtr, int row, int column) { - CursorWindow_host instance = sInstances.get(windowPtr); - if (row >= instance.mRows.size() || column >= instance.mColumnNum) { - return null; - } - - return instance.mRows.get(row).mFields[column]; - } - - public static boolean nativePutLong(long windowPtr, long value, int row, int column) { - return put(windowPtr, Long.toString(value), Cursor.FIELD_TYPE_INTEGER, row, column); - } - - public static long nativeGetLong(long windowPtr, int row, int column) { - String value = nativeGetString(windowPtr, row, column); - if (value == null) { - return 0; - } - - Number number = new DecimalFormat().parse(value, new ParsePosition(0)); - return number == null ? 0 : number.longValue(); - } - - public static boolean nativePutDouble(long windowPtr, double value, int row, int column) { - return put(windowPtr, Double.toString(value), Cursor.FIELD_TYPE_FLOAT, row, column); - } - - public static double nativeGetDouble(long windowPtr, int row, int column) { - String value = nativeGetString(windowPtr, row, column); - if (value == null) { - return 0; - } - - Number number = new DecimalFormat().parse(value, new ParsePosition(0)); - return number == null ? 0 : number.doubleValue(); - } - - public static boolean nativePutBlob(long windowPtr, byte[] value, int row, int column) { - return put(windowPtr, value == null ? null : Base64.encodeToString(value, 0), - Cursor.FIELD_TYPE_BLOB, row, column); - } - - public static byte[] nativeGetBlob(long windowPtr, int row, int column) { - int type = nativeGetType(windowPtr, row, column); - switch (type) { - case Cursor.FIELD_TYPE_BLOB: { - String value = nativeGetString(windowPtr, row, column); - return value == null ? null : Base64.decode(value, 0); - } - case Cursor.FIELD_TYPE_STRING: { - String value = nativeGetString(windowPtr, row, column); - return value == null ? null : value.getBytes(); - } - case Cursor.FIELD_TYPE_FLOAT: - throw new SQLiteException(); - case Cursor.FIELD_TYPE_INTEGER: - throw new SQLiteException(); - case Cursor.FIELD_TYPE_NULL: - default: - return null; - } - } - - public static void nativeWriteToParcel(long windowPtr, Parcel parcel) { - CursorWindow_host window = sInstances.get(windowPtr); - parcel.writeString(window.mName); - parcel.writeInt(window.mColumnNum); - parcel.writeInt(window.mRows.size()); - for (int row = 0; row < window.mRows.size(); row++) { - parcel.writeStringArray(window.mRows.get(row).mFields); - parcel.writeIntArray(window.mRows.get(row).mTypes); - } - } - - public static long nativeCreateFromParcel(Parcel parcel) { - long windowPtr = nativeCreate(null, 0); - CursorWindow_host window = sInstances.get(windowPtr); - window.mName = parcel.readString(); - window.mColumnNum = parcel.readInt(); - int rowCount = parcel.readInt(); - for (int row = 0; row < rowCount; row++) { - Row r = new Row(); - r.mFields = parcel.createStringArray(); - r.mTypes = parcel.createIntArray(); - window.mRows.add(r); - } - return windowPtr; - } -} diff --git a/ravenwood/runtime-helper-src/framework/android/os/MessageQueue_host.java b/ravenwood/runtime-helper-src/framework/android/os/MessageQueue_host.java deleted file mode 100644 index 1b63adc4319f..000000000000 --- a/ravenwood/runtime-helper-src/framework/android/os/MessageQueue_host.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright (C) 2023 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.os; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicLong; - -public class MessageQueue_host { - private static final AtomicLong sNextId = new AtomicLong(1); - private static final Map<Long, MessageQueue_host> sInstances = new ConcurrentHashMap<>(); - - private boolean mDeleted = false; - - private final Object mPoller = new Object(); - private volatile boolean mPolling; - private volatile boolean mPendingWake; - - private void validate() { - if (mDeleted) { - // TODO: Put more info - throw new RuntimeException("MessageQueue already destroyed"); - } - } - - private static MessageQueue_host getInstance(long id) { - MessageQueue_host q = sInstances.get(id); - if (q == null) { - throw new RuntimeException("MessageQueue doesn't exist with id=" + id); - } - q.validate(); - return q; - } - - public static long nativeInit() { - final long id = sNextId.getAndIncrement(); - final MessageQueue_host q = new MessageQueue_host(); - sInstances.put(id, q); - return id; - } - - public static void nativeDestroy(long ptr) { - getInstance(ptr).mDeleted = true; - sInstances.remove(ptr); - } - - public static void nativePollOnce(android.os.MessageQueue queue, long ptr, int timeoutMillis) { - var q = getInstance(ptr); - synchronized (q.mPoller) { - q.mPolling = true; - try { - if (q.mPendingWake) { - // Calling with pending wake returns immediately - } else if (timeoutMillis == 0) { - // Calling epoll_wait() with 0 returns immediately - } else if (timeoutMillis == -1) { - q.mPoller.wait(); - } else { - q.mPoller.wait(timeoutMillis); - } - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - // Any reason for returning counts as a "wake", so clear pending - q.mPendingWake = false; - q.mPolling = false; - } - } - - public static void nativeWake(long ptr) { - var q = getInstance(ptr); - synchronized (q.mPoller) { - q.mPendingWake = true; - q.mPoller.notifyAll(); - } - } - - public static boolean nativeIsPolling(long ptr) { - var q = getInstance(ptr); - return q.mPolling; - } - - public static void nativeSetFileDescriptorEvents(long ptr, int fd, int events) { - throw new UnsupportedOperationException(); - } -} diff --git a/ravenwood/runtime-helper-src/framework/com/android/internal/os/LongArrayContainer_host.java b/ravenwood/runtime-helper-src/framework/com/android/internal/os/LongArrayContainer_host.java deleted file mode 100644 index c18c307ad1e3..000000000000 --- a/ravenwood/runtime-helper-src/framework/com/android/internal/os/LongArrayContainer_host.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (C) 2024 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.android.internal.os; - -import java.util.Arrays; -import java.util.HashMap; - -public class LongArrayContainer_host { - private static final HashMap<Long, long[]> sInstances = new HashMap<>(); - private static long sNextId = 1; - - public static long native_init(int arrayLength) { - long[] array = new long[arrayLength]; - long instanceId = sNextId++; - sInstances.put(instanceId, array); - return instanceId; - } - - static long[] getInstance(long instanceId) { - return sInstances.get(instanceId); - } - - public static void native_setValues(long instanceId, long[] values) { - System.arraycopy(values, 0, getInstance(instanceId), 0, values.length); - } - - public static void native_getValues(long instanceId, long[] values) { - System.arraycopy(getInstance(instanceId), 0, values, 0, values.length); - } - - public static boolean native_combineValues(long instanceId, long[] array, int[] indexMap) { - long[] values = getInstance(instanceId); - - boolean nonZero = false; - Arrays.fill(array, 0); - - for (int i = 0; i < values.length; i++) { - int index = indexMap[i]; - if (index < 0 || index >= array.length) { - throw new IndexOutOfBoundsException("Index " + index + " is out of bounds: [0, " - + (array.length - 1) + "]"); - } - if (values[i] != 0) { - array[index] += values[i]; - nonZero = true; - } - } - return nonZero; - } -} diff --git a/ravenwood/runtime-helper-src/framework/com/android/internal/os/LongArrayMultiStateCounter_host.java b/ravenwood/runtime-helper-src/framework/com/android/internal/os/LongArrayMultiStateCounter_host.java deleted file mode 100644 index 9ce8ea8e16ef..000000000000 --- a/ravenwood/runtime-helper-src/framework/com/android/internal/os/LongArrayMultiStateCounter_host.java +++ /dev/null @@ -1,355 +0,0 @@ -/* - * Copyright (C) 2023 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.internal.os; - -import android.os.BadParcelableException; -import android.os.Parcel; - -import java.util.Arrays; -import java.util.HashMap; - -/** - * Native implementation substitutions for the LongArrayMultiStateCounter class. - */ -public class LongArrayMultiStateCounter_host { - - /** - * A reimplementation of {@link LongArrayMultiStateCounter}, only in - * Java instead of native. The majority of the code (in C++) can be found in - * /frameworks/native/libs/battery/MultiStateCounter.h - */ - private static class LongArrayMultiStateCounterRavenwood { - private final int mStateCount; - private final int mArrayLength; - private int mCurrentState; - private long mLastStateChangeTimestampMs = -1; - private long mLastUpdateTimestampMs = -1; - private boolean mEnabled = true; - - private static class State { - private long mTimeInStateSinceUpdate; - private long[] mCounter; - } - - private final State[] mStates; - private final long[] mValues; - private final long[] mDelta; - - LongArrayMultiStateCounterRavenwood(int stateCount, int arrayLength) { - mStateCount = stateCount; - mArrayLength = arrayLength; - mStates = new State[stateCount]; - for (int i = 0; i < mStateCount; i++) { - mStates[i] = new State(); - mStates[i].mCounter = new long[mArrayLength]; - } - mValues = new long[mArrayLength]; - mDelta = new long[mArrayLength]; - } - - public void setEnabled(boolean enabled, long timestampMs) { - if (enabled == mEnabled) { - return; - } - - if (!enabled) { - setState(mCurrentState, timestampMs); - mEnabled = false; - } else { - if (timestampMs < mLastUpdateTimestampMs) { - timestampMs = mLastUpdateTimestampMs; - } - - if (mLastStateChangeTimestampMs >= 0) { - mLastStateChangeTimestampMs = timestampMs; - } - mEnabled = true; - } - } - - public void setState(int state, long timestampMs) { - if (mEnabled && mLastStateChangeTimestampMs >= 0 && mLastUpdateTimestampMs >= 0) { - if (timestampMs < mLastUpdateTimestampMs) { - timestampMs = mLastUpdateTimestampMs; - } - - if (timestampMs >= mLastStateChangeTimestampMs) { - mStates[mCurrentState].mTimeInStateSinceUpdate += - timestampMs - mLastStateChangeTimestampMs; - } else { - for (int i = 0; i < mStateCount; i++) { - mStates[i].mTimeInStateSinceUpdate = 0; - } - } - } - mCurrentState = state; - mLastStateChangeTimestampMs = timestampMs; - } - - public void copyStatesFrom(LongArrayMultiStateCounterRavenwood source) { - for (int i = 0; i < mStateCount; i++) { - mStates[i].mTimeInStateSinceUpdate = source.mStates[i].mTimeInStateSinceUpdate; - Arrays.fill(mStates[i].mCounter, 0); - } - mCurrentState = source.mCurrentState; - mLastStateChangeTimestampMs = source.mLastStateChangeTimestampMs; - mLastUpdateTimestampMs = source.mLastUpdateTimestampMs; - } - - public void setValue(int state, long[] values) { - System.arraycopy(values, 0, mStates[state].mCounter, 0, mArrayLength); - } - - public void updateValue(long[] values, long timestampMs) { - if (mEnabled || mLastUpdateTimestampMs < mLastStateChangeTimestampMs) { - if (timestampMs < mLastStateChangeTimestampMs) { - timestampMs = mLastStateChangeTimestampMs; - } - - setState(mCurrentState, timestampMs); - - if (mLastUpdateTimestampMs >= 0) { - if (timestampMs > mLastUpdateTimestampMs) { - if (delta(mValues, values, mDelta)) { - long timeSinceUpdate = timestampMs - mLastUpdateTimestampMs; - for (int i = 0; i < mStateCount; i++) { - long timeInState = mStates[i].mTimeInStateSinceUpdate; - if (timeInState > 0) { - add(mStates[i].mCounter, mDelta, timeInState, timeSinceUpdate); - mStates[i].mTimeInStateSinceUpdate = 0; - } - } - } else { - throw new RuntimeException(); - } - } else if (timestampMs < mLastUpdateTimestampMs) { - throw new RuntimeException(); - } - } - } - System.arraycopy(values, 0, mValues, 0, mArrayLength); - mLastUpdateTimestampMs = timestampMs; - } - - public void incrementValues(long[] delta, long timestampMs) { - long[] values = Arrays.copyOf(mValues, mValues.length); - for (int i = 0; i < mArrayLength; i++) { - values[i] += delta[i]; - } - updateValue(values, timestampMs); - } - - public void addCounts(long[] delta) { - if (!mEnabled) { - return; - } - - for (int i = 0; i < mArrayLength; i++) { - mStates[mCurrentState].mCounter[i] += delta[i]; - } - } - - public void getValues(long[] values, int state) { - System.arraycopy(mStates[state].mCounter, 0, values, 0, mArrayLength); - } - - public void reset() { - mLastStateChangeTimestampMs = -1; - mLastUpdateTimestampMs = -1; - for (int i = 0; i < mStateCount; i++) { - mStates[i].mTimeInStateSinceUpdate = 0; - Arrays.fill(mStates[i].mCounter, 0); - } - } - - public void writeToParcel(Parcel parcel) { - parcel.writeInt(mStateCount); - parcel.writeInt(mArrayLength); - for (int i = 0; i < mStateCount; i++) { - parcel.writeLongArray(mStates[i].mCounter); - } - } - - public void initFromParcel(Parcel parcel) { - try { - for (int i = 0; i < mStateCount; i++) { - parcel.readLongArray(mStates[i].mCounter); - } - } catch (Exception e) { - throw new BadParcelableException(e); - } - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("["); - for (int state = 0; state < mStateCount; state++) { - if (state != 0) { - sb.append(", "); - } - sb.append(state).append(": {"); - for (int i = 0; i < mStates[state].mCounter.length; i++) { - if (i != 0) { - sb.append(", "); - } - sb.append(mStates[state].mCounter[i]); - } - sb.append("}"); - } - sb.append("]"); - if (mLastUpdateTimestampMs >= 0) { - sb.append(" updated: ").append(mLastUpdateTimestampMs); - } - if (mLastStateChangeTimestampMs >= 0) { - sb.append(" currentState: ").append(mCurrentState); - if (mLastStateChangeTimestampMs > mLastUpdateTimestampMs) { - sb.append(" stateChanged: ").append(mLastStateChangeTimestampMs); - } - } else { - sb.append(" currentState: none"); - } - return sb.toString(); - } - - private boolean delta(long[] values1, long[] values2, long[] delta) { - if (delta.length != mArrayLength) { - throw new RuntimeException(); - } - - boolean is_delta_valid = true; - for (int i = 0; i < mArrayLength; i++) { - if (values2[i] >= values1[i]) { - delta[i] = values2[i] - values1[i]; - } else { - delta[i] = 0; - is_delta_valid = false; - } - } - - return is_delta_valid; - } - - private void add(long[] counter, long[] delta, long numerator, long denominator) { - if (numerator != denominator) { - for (int i = 0; i < mArrayLength; i++) { - counter[i] += delta[i] * numerator / denominator; - } - } else { - for (int i = 0; i < mArrayLength; i++) { - counter[i] += delta[i]; - } - } - } - } - - private static final HashMap<Long, LongArrayMultiStateCounterRavenwood> sInstances = - new HashMap<>(); - private static long sNextId = 1; - - public static long native_init(int stateCount, int arrayLength) { - LongArrayMultiStateCounterRavenwood instance = new LongArrayMultiStateCounterRavenwood( - stateCount, arrayLength); - long instanceId = sNextId++; - sInstances.put(instanceId, instance); - return instanceId; - } - - private static LongArrayMultiStateCounterRavenwood getInstance(long instanceId) { - return sInstances.get(instanceId); - } - - public static void native_setEnabled(long instanceId, boolean enabled, - long timestampMs) { - getInstance(instanceId).setEnabled(enabled, timestampMs); - } - - public static int native_getStateCount(long instanceId) { - return getInstance(instanceId).mStateCount; - } - - public static int native_getArrayLength(long instanceId) { - return getInstance(instanceId).mArrayLength; - } - - public static void native_setValues(long instanceId, int state, long containerInstanceId) { - getInstance(instanceId).setValue(state, - LongArrayContainer_host.getInstance(containerInstanceId)); - } - - public static void native_updateValues(long instanceId, long containerInstanceId, - long timestampMs) { - getInstance(instanceId).updateValue( - LongArrayContainer_host.getInstance(containerInstanceId), timestampMs); - } - - public static void native_setState(long instanceId, int state, long timestampMs) { - getInstance(instanceId).setState(state, timestampMs); - } - - public static void native_copyStatesFrom(long targetInstanceId, long sourceInstanceId) { - getInstance(targetInstanceId).copyStatesFrom(getInstance(sourceInstanceId)); - } - - public static void native_incrementValues(long instanceId, long containerInstanceId, - long timestampMs) { - getInstance(instanceId).incrementValues( - LongArrayContainer_host.getInstance(containerInstanceId), timestampMs); - } - - public static void native_addCounts(long instanceId, long containerInstanceId) { - getInstance(instanceId).addCounts(LongArrayContainer_host.getInstance(containerInstanceId)); - } - - public static void native_getCounts(long instanceId, long containerInstanceId, int state) { - getInstance(instanceId).getValues(LongArrayContainer_host.getInstance(containerInstanceId), - state); - } - - public static void native_reset(long instanceId) { - getInstance(instanceId).reset(); - } - - public static void native_writeToParcel(long instanceId, Parcel parcel, int flags) { - getInstance(instanceId).writeToParcel(parcel); - } - - public static long native_initFromParcel(Parcel parcel) { - int stateCount = parcel.readInt(); - if (stateCount < 0 || stateCount > 0xEFFF) { - throw new BadParcelableException("stateCount out of range"); - } - // LongArrayMultiStateCounter.cpp uses AParcel, which throws on out-of-data. - if (parcel.dataPosition() >= parcel.dataSize()) { - throw new RuntimeException("Bad parcel"); - } - int arrayLength = parcel.readInt(); - if (parcel.dataPosition() >= parcel.dataSize()) { - throw new RuntimeException("Bad parcel"); - } - long instanceId = native_init(stateCount, arrayLength); - getInstance(instanceId).initFromParcel(parcel); - if (parcel.dataPosition() > parcel.dataSize()) { - throw new RuntimeException("Bad parcel"); - } - return instanceId; - } - - public static String native_toString(long instanceId) { - return getInstance(instanceId).toString(); - } -} diff --git a/ravenwood/runtime-helper-src/framework/com/android/internal/os/LongMultiStateCounter_host.java b/ravenwood/runtime-helper-src/framework/com/android/internal/os/LongMultiStateCounter_host.java deleted file mode 100644 index 1d95aa143549..000000000000 --- a/ravenwood/runtime-helper-src/framework/com/android/internal/os/LongMultiStateCounter_host.java +++ /dev/null @@ -1,263 +0,0 @@ -/* - * Copyright (C) 2024 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.internal.os; - -import android.os.BadParcelableException; -import android.os.Parcel; - -import java.util.HashMap; - -/** - * Native implementation substitutions for the LongMultiStateCounter class. - */ -public class LongMultiStateCounter_host { - - /** - * A reimplementation of {@link com.android.internal.os.LongMultiStateCounter}, only in - * Java instead of native. The majority of the code (in C++) can be found in - * /frameworks/native/libs/battery/MultiStateCounter.h - */ - private static class LongMultiStateCounterRavenwood { - private final int mStateCount; - private int mCurrentState; - private long mLastStateChangeTimestampMs = -1; - private long mLastUpdateTimestampMs = -1; - private boolean mEnabled = true; - - private static class State { - private long mTimeInStateSinceUpdate; - private long mCounter; - } - - private final State[] mStates; - private long mValue; - - LongMultiStateCounterRavenwood(int stateCount) { - mStateCount = stateCount; - mStates = new State[stateCount]; - for (int i = 0; i < mStateCount; i++) { - mStates[i] = new State(); - } - } - - public void setEnabled(boolean enabled, long timestampMs) { - if (enabled == mEnabled) { - return; - } - - if (!enabled) { - setState(mCurrentState, timestampMs); - mEnabled = false; - } else { - if (timestampMs < mLastUpdateTimestampMs) { - timestampMs = mLastUpdateTimestampMs; - } - - if (mLastStateChangeTimestampMs >= 0) { - mLastStateChangeTimestampMs = timestampMs; - } - mEnabled = true; - } - } - - public void setState(int state, long timestampMs) { - if (mEnabled && mLastStateChangeTimestampMs >= 0 && mLastUpdateTimestampMs >= 0) { - if (timestampMs < mLastUpdateTimestampMs) { - timestampMs = mLastUpdateTimestampMs; - } - - if (timestampMs >= mLastStateChangeTimestampMs) { - mStates[mCurrentState].mTimeInStateSinceUpdate += - timestampMs - mLastStateChangeTimestampMs; - } else { - for (int i = 0; i < mStateCount; i++) { - mStates[i].mTimeInStateSinceUpdate = 0; - } - } - } - mCurrentState = state; - mLastStateChangeTimestampMs = timestampMs; - } - - public long updateValue(long value, long timestampMs) { - long returnValue = 0; - if (mEnabled || mLastUpdateTimestampMs < mLastStateChangeTimestampMs) { - if (timestampMs < mLastStateChangeTimestampMs) { - timestampMs = mLastStateChangeTimestampMs; - } - - setState(mCurrentState, timestampMs); - - if (mLastUpdateTimestampMs >= 0) { - if (timestampMs > mLastUpdateTimestampMs) { - long delta = value - mValue; - if (delta >= 0) { - returnValue = delta; - long timeSinceUpdate = timestampMs - mLastUpdateTimestampMs; - for (int i = 0; i < mStateCount; i++) { - long timeInState = mStates[i].mTimeInStateSinceUpdate; - if (timeInState > 0) { - mStates[i].mCounter += delta * timeInState / timeSinceUpdate; - mStates[i].mTimeInStateSinceUpdate = 0; - } - } - } else { - for (int i = 0; i < mStateCount; i++) { - mStates[i].mTimeInStateSinceUpdate = 0; - } - } - } else if (timestampMs < mLastUpdateTimestampMs) { - for (int i = 0; i < mStateCount; i++) { - mStates[i].mTimeInStateSinceUpdate = 0; - } - } - } - } - mValue = value; - mLastUpdateTimestampMs = timestampMs; - return returnValue; - } - - public void incrementValue(long count, long timestampMs) { - updateValue(mValue + count, timestampMs); - } - - public long getValue(int state) { - return mStates[state].mCounter; - } - - public void reset() { - mLastStateChangeTimestampMs = -1; - mLastUpdateTimestampMs = -1; - for (int i = 0; i < mStateCount; i++) { - mStates[i].mTimeInStateSinceUpdate = 0; - mStates[i].mCounter = 0; - } - } - - public void writeToParcel(Parcel parcel) { - parcel.writeInt(mStateCount); - for (int i = 0; i < mStateCount; i++) { - parcel.writeLong(mStates[i].mCounter); - } - } - - public void initFromParcel(Parcel parcel) { - try { - for (int i = 0; i < mStateCount; i++) { - mStates[i].mCounter = parcel.readLong(); - } - } catch (Exception e) { - throw new BadParcelableException(e); - } - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("["); - for (int state = 0; state < mStateCount; state++) { - if (state != 0) { - sb.append(", "); - } - sb.append(state).append(": ").append(mStates[state].mCounter); - } - sb.append("]"); - if (mLastUpdateTimestampMs >= 0) { - sb.append(" updated: ").append(mLastUpdateTimestampMs); - } - if (mLastStateChangeTimestampMs >= 0) { - sb.append(" currentState: ").append(mCurrentState); - if (mLastStateChangeTimestampMs > mLastUpdateTimestampMs) { - sb.append(" stateChanged: ").append(mLastStateChangeTimestampMs); - } - } else { - sb.append(" currentState: none"); - } - return sb.toString(); - } - } - - private static final HashMap<Long, LongMultiStateCounterRavenwood> sInstances = - new HashMap<>(); - private static long sNextId = 1; - - public static long native_init(int stateCount) { - LongMultiStateCounterRavenwood instance = new LongMultiStateCounterRavenwood(stateCount); - long instanceId = sNextId++; - sInstances.put(instanceId, instance); - return instanceId; - } - - private static LongMultiStateCounterRavenwood getInstance(long instanceId) { - return sInstances.get(instanceId); - } - - public static void native_setEnabled(long instanceId, boolean enabled, - long timestampMs) { - getInstance(instanceId).setEnabled(enabled, timestampMs); - } - - public static int native_getStateCount(long instanceId) { - return getInstance(instanceId).mStateCount; - } - - public static long native_updateValue(long instanceId, long value, long timestampMs) { - return getInstance(instanceId).updateValue(value, timestampMs); - } - - public static void native_setState(long instanceId, int state, long timestampMs) { - getInstance(instanceId).setState(state, timestampMs); - } - - public static void native_incrementValue(long instanceId, long count, long timestampMs) { - getInstance(instanceId).incrementValue(count, timestampMs); - } - - public static long native_getCount(long instanceId, int state) { - return getInstance(instanceId).getValue(state); - } - - public static void native_reset(long instanceId) { - getInstance(instanceId).reset(); - } - - public static void native_writeToParcel(long instanceId, Parcel parcel, int flags) { - getInstance(instanceId).writeToParcel(parcel); - } - - public static long native_initFromParcel(Parcel parcel) { - int stateCount = parcel.readInt(); - if (stateCount < 0 || stateCount > 0xEFFF) { - throw new BadParcelableException("stateCount out of range"); - } - // LongMultiStateCounter.cpp uses AParcel, which throws on out-of-data. - if (parcel.dataPosition() >= parcel.dataSize()) { - throw new RuntimeException("Bad parcel"); - } - long instanceId = native_init(stateCount); - getInstance(instanceId).initFromParcel(parcel); - if (parcel.dataPosition() > parcel.dataSize()) { - throw new RuntimeException("Bad parcel"); - } - return instanceId; - } - - public static String native_toString(long instanceId) { - return getInstance(instanceId).toString(); - } -} |