diff options
Diffstat (limited to 'android/app/usage/TimeSparseArray.java')
-rw-r--r-- | android/app/usage/TimeSparseArray.java | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/android/app/usage/TimeSparseArray.java b/android/app/usage/TimeSparseArray.java index 7974fa70..9ef88e41 100644 --- a/android/app/usage/TimeSparseArray.java +++ b/android/app/usage/TimeSparseArray.java @@ -17,6 +17,7 @@ package android.app.usage; import android.util.LongSparseArray; +import android.util.Slog; /** * An array that indexes by a long timestamp, representing milliseconds since the epoch. @@ -24,6 +25,8 @@ import android.util.LongSparseArray; * {@hide} */ public class TimeSparseArray<E> extends LongSparseArray<E> { + private static final String TAG = TimeSparseArray.class.getSimpleName(); + public TimeSparseArray() { super(); } @@ -70,6 +73,30 @@ public class TimeSparseArray<E> extends LongSparseArray<E> { } /** + * {@inheritDoc} + * + * Overridden to ensure no collisions. The key (time in milliseconds) is incremented till an + * empty place is found. + */ + @Override + public void put(long key, E value) { + final long origKey = key; + int keyIndex = indexOfKey(key); + if (keyIndex >= 0) { + final long sz = size(); + while (keyIndex < sz && keyAt(keyIndex) == key) { + key++; + keyIndex++; + } + if (key >= origKey + 10) { + Slog.w(TAG, "Value " + value + " supposed to be inserted at " + origKey + + " displaced to " + key); + } + } + super.put(key, value); + } + + /** * Finds the index of the first element whose timestamp is less than or equal to * the given time. * |