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

Commit a8a0435d authored by Jake Wharton's avatar Jake Wharton
Browse files

Expose a few APIs in util collections.

These are either already exposed on other specialized collection variants or are exposed as public API on the androidx versions, or both.

With these APIs exposed, all of the unsupported app usage can be done through public API. As a result, all unsupported app usage is now locked to apps targeting API 28 or earlier.

Bug: 116877302
Test: none, no implementation change
Change-Id: I548d71319bffb0a6b529e380ea936df674dbf515
parent cc9e174e
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -45696,6 +45696,7 @@ package android.util {
    method public java.util.Set<java.util.Map.Entry<K, V>> entrySet();
    method public V get(java.lang.Object);
    method public int indexOfKey(java.lang.Object);
    method public int indexOfValue(java.lang.Object);
    method public boolean isEmpty();
    method public K keyAt(int);
    method public java.util.Set<K> keySet();
@@ -45716,6 +45717,7 @@ package android.util {
    ctor public ArraySet();
    ctor public ArraySet(int);
    ctor public ArraySet(android.util.ArraySet<E>);
    ctor public ArraySet(java.util.Collection<? extends E>);
    method public boolean add(E);
    method public void addAll(android.util.ArraySet<? extends E>);
    method public boolean addAll(java.util.Collection<? extends E>);
@@ -46287,6 +46289,7 @@ package android.util {
    method public int keyAt(int);
    method public void put(int, boolean);
    method public void removeAt(int);
    method public void setValueAt(int, boolean);
    method public int size();
    method public boolean valueAt(int);
  }
@@ -46305,6 +46308,7 @@ package android.util {
    method public int keyAt(int);
    method public void put(int, int);
    method public void removeAt(int);
    method public void setValueAt(int, int);
    method public int size();
    method public int valueAt(int);
  }
+24 −17
Original line number Diff line number Diff line
@@ -71,19 +71,19 @@ public final class ArrayMap<K, V> implements Map<K, V> {
    /**
     * Maximum number of entries to have in array caches.
     */
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Allocations are an implementation detail.
    private static final int CACHE_SIZE = 10;

    /**
     * Special hash array value that indicates the container is immutable.
     */
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Allocations are an implementation detail.
    static final int[] EMPTY_IMMUTABLE_INTS = new int[0];

    /**
     * @hide Special immutable empty ArrayMap.
     */
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Use your own singleton empty map.
    public static final ArrayMap EMPTY = new ArrayMap<>(-1);

    /**
@@ -92,21 +92,21 @@ public final class ArrayMap<K, V> implements Map<K, V> {
     * The first entry in the array is a pointer to the next array in the
     * list; the second entry is a pointer to the int[] hash code array for it.
     */
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Allocations are an implementation detail.
    static Object[] mBaseCache;
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Allocations are an implementation detail.
    static int mBaseCacheSize;
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Allocations are an implementation detail.
    static Object[] mTwiceBaseCache;
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Allocations are an implementation detail.
    static int mTwiceBaseCacheSize;

    final boolean mIdentityHashCode;
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Hashes are an implementation detail. Use public key/value API.
    int[] mHashes;
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Storage is an implementation detail. Use public key/value API.
    Object[] mArray;
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Use size()
    int mSize;
    MapCollections<K, V> mCollections;

@@ -122,7 +122,7 @@ public final class ArrayMap<K, V> implements Map<K, V> {
        }
    }

    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Hashes are an implementation detail. Use indexOfKey(Object).
    int indexOf(Object key, int hash) {
        final int N = mSize;

@@ -161,7 +161,7 @@ public final class ArrayMap<K, V> implements Map<K, V> {
        return ~end;
    }

    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Use indexOf(null)
    int indexOfNull() {
        final int N = mSize;

@@ -200,7 +200,7 @@ public final class ArrayMap<K, V> implements Map<K, V> {
        return ~end;
    }

    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Allocations are an implementation detail.
    private void allocArrays(final int size) {
        if (mHashes == EMPTY_IMMUTABLE_INTS) {
            throw new UnsupportedOperationException("ArrayMap is immutable");
@@ -239,7 +239,7 @@ public final class ArrayMap<K, V> implements Map<K, V> {
        mArray = new Object[size<<1];
    }

    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Allocations are an implementation detail.
    private static void freeArrays(final int[] hashes, final Object[] array, final int size) {
        if (hashes.length == (BASE_SIZE*2)) {
            synchronized (ArrayMap.class) {
@@ -393,8 +393,15 @@ public final class ArrayMap<K, V> implements Map<K, V> {
                : indexOf(key, mIdentityHashCode ? System.identityHashCode(key) : key.hashCode());
    }

    @UnsupportedAppUsage
    int indexOfValue(Object value) {
    /**
     * Returns an index for which {@link #valueAt} would return the
     * specified value, or a negative number if no keys map to the
     * specified value.
     * Beware that this is a linear search, unlike lookups by key,
     * and that multiple keys can map to the same value and this will
     * find only one of them.
     */
    public int indexOfValue(Object value) {
        final int N = mSize*2;
        final Object[] array = mArray;
        if (value == null) {
@@ -551,7 +558,7 @@ public final class ArrayMap<K, V> implements Map<K, V> {
     * The array must already be large enough to contain the item.
     * @hide
     */
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Storage is an implementation detail. Use put(K, V).
    public void append(K key, V value) {
        int index = mSize;
        final int hash = key == null ? 0
+11 −10
Original line number Diff line number Diff line
@@ -71,15 +71,15 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
    static int sTwiceBaseCacheSize;

    final boolean mIdentityHashCode;
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Hashes are an implementation detail. Use public API.
    int[] mHashes;
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Storage is an implementation detail. Use public API.
    Object[] mArray;
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Use size()
    int mSize;
    MapCollections<E, E> mCollections;

    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Hashes are an implementation detail. Use indexOfKey(Object).
    private int indexOf(Object key, int hash) {
        final int N = mSize;

@@ -118,7 +118,7 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
        return ~end;
    }

    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Use indexOf(null)
    private int indexOfNull() {
        final int N = mSize;

@@ -157,7 +157,7 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
        return ~end;
    }

    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Allocations are an implementation detail.
    private void allocArrays(final int size) {
        if (size == (BASE_SIZE * 2)) {
            synchronized (ArraySet.class) {
@@ -215,7 +215,7 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
        mArray = new Object[size];
    }

    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Allocations are an implementation detail.
    private static void freeArrays(final int[] hashes, final Object[] array, final int size) {
        if (hashes.length == (BASE_SIZE * 2)) {
            synchronized (ArraySet.class) {
@@ -289,9 +289,10 @@ public final class ArraySet<E> implements Collection<E>, Set<E> {
        }
    }

    /** {@hide} */
    @UnsupportedAppUsage
    public ArraySet(Collection<E> set) {
    /**
     * Create a new ArraySet with items from the given collection.
     */
    public ArraySet(Collection<? extends E> set) {
        this();
        if (set != null) {
            addAll(set);
+3 −3
Original line number Diff line number Diff line
@@ -46,11 +46,11 @@ import libcore.util.EmptyArray;
 * @hide
 */
public class LongSparseLongArray implements Cloneable {
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // The type isn't even public.
    private long[] mKeys;
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // The type isn't even public.
    private long[] mValues;
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // The type isn't even public.
    private int mSize;

    /**
+3 −3
Original line number Diff line number Diff line
@@ -56,11 +56,11 @@ public class SparseArray<E> implements Cloneable {
    private static final Object DELETED = new Object();
    private boolean mGarbage = false;

    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Use keyAt(int)
    private int[] mKeys;
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Use valueAt(int), setValueAt(int, E)
    private Object[] mValues;
    @UnsupportedAppUsage
    @UnsupportedAppUsage(maxTargetSdk = 28) // Use size()
    private int mSize;

    /**
Loading