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

Commit 01a9adde authored by Svetoslav Ganov's avatar Svetoslav Ganov Committed by Android (Google) Code Review
Browse files

Merge "Adding pool management via the poolable APIs to some classes."

parents 53018e52 be922dc9
Loading
Loading
Loading
Loading
+4 −13
Original line number Diff line number Diff line
@@ -21,8 +21,8 @@ package android.util;
 * <pre>
 * public class MyPooledClass {
 *
 *     private static final Pool<MyPooledClass> sPool =
 *             new SynchronizedPool<MyPooledClass>(Pools.POOL_SIZE_INFINITE);
 *     private static final SynchronizedPool<MyPooledClass> sPool =
 *             new SynchronizedPool<MyPooledClass>(10);
 *
 *     public static MyPooledClass obtain() {
 *         MyPooledClass instance = sPool.acquire();
@@ -42,11 +42,6 @@ package android.util;
 */
public final class Pools {

    /**
     * Pool with an infinite size.
     */
    public static final int POOL_SIZE_INFINITE = -1;

    /**
     * Interface for managing a pool of objects.
     *
@@ -98,11 +93,9 @@ public final class Pools {
         * @param maxPoolSize The max pool size.
         *
         * @throws IllegalArgumentException If the max pool size is less than zero.
         *
         * @see Pools#POOL_SIZE_INFINITE
         */
        public SimplePool(int maxPoolSize) {
            if (maxPoolSize <= 0 && maxPoolSize != POOL_SIZE_INFINITE) {
            if (maxPoolSize <= 0) {
                throw new IllegalArgumentException("The max pool size must be > 0");
            }
            mMaxPoolSize = maxPoolSize;
@@ -128,7 +121,7 @@ public final class Pools {
            if (isInPool(instance)) {
                throw new IllegalStateException("Already in the pool!");
            }
            if (mMaxPoolSize == POOL_SIZE_INFINITE || mPoolSize < mMaxPoolSize) {
            if (mPoolSize < mMaxPoolSize) {
                PoolableHolder<T> holder = mEmptyHolders;
                if (holder == null) {
                    holder = new PoolableHolder<T>();
@@ -170,8 +163,6 @@ public final class Pools {
         * @param maxPoolSize The max pool size.
         *
         * @throws IllegalArgumentException If the max pool size is less than zero.
         *
         * @see Pools#POOL_SIZE_INFINITE
         */
        public SynchronizedPool(int maxPoolSize) {
            super(maxPoolSize);
+17 −68
Original line number Diff line number Diff line
@@ -23,11 +23,9 @@ import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Insets;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
@@ -37,6 +35,7 @@ import android.os.Parcelable;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Pools.SynchronizedPool;
import android.util.SparseArray;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
@@ -6234,50 +6233,25 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager

        private static final int MAX_POOL_SIZE = 32;

        private static final Object sPoolLock = new Object();

        private static ChildListForAccessibility sPool;

        private static int sPoolSize;

        private boolean mIsPooled;

        private ChildListForAccessibility mNext;
        private static final SynchronizedPool<ChildListForAccessibility> sPool =
                new SynchronizedPool<ChildListForAccessibility>(MAX_POOL_SIZE);

        private final ArrayList<View> mChildren = new ArrayList<View>();

        private final ArrayList<ViewLocationHolder> mHolders = new ArrayList<ViewLocationHolder>();

        public static ChildListForAccessibility obtain(ViewGroup parent, boolean sort) {
            ChildListForAccessibility list = null;
            synchronized (sPoolLock) {
                if (sPool != null) {
                    list = sPool;
                    sPool = list.mNext;
                    list.mNext = null;
                    list.mIsPooled = false;
                    sPoolSize--;
                } else {
            ChildListForAccessibility list = sPool.acquire();
            if (list == null) {
                list = new ChildListForAccessibility();
            }
            list.init(parent, sort);
            return list;
        }
        }

        public void recycle() {
            if (mIsPooled) {
                throw new IllegalStateException("Instance already recycled.");
            }
            clear();
            synchronized (sPoolLock) {
                if (sPoolSize < MAX_POOL_SIZE) {
                    mNext = sPool;
                    mIsPooled = true;
                    sPool = this;
                    sPoolSize++;
                }
            }
            sPool.release(this);
        }

        public int getChildCount() {
@@ -6331,15 +6305,8 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager

        private static final int MAX_POOL_SIZE = 32;

        private static final Object sPoolLock = new Object();

        private static ViewLocationHolder sPool;

        private static int sPoolSize;

        private boolean mIsPooled;

        private ViewLocationHolder mNext;
        private static final SynchronizedPool<ViewLocationHolder> sPool =
                new SynchronizedPool<ViewLocationHolder>(MAX_POOL_SIZE);

        private final Rect mLocation = new Rect();

@@ -6348,35 +6315,17 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
        private int mLayoutDirection;

        public static ViewLocationHolder obtain(ViewGroup root, View view) {
            ViewLocationHolder holder = null;
            synchronized (sPoolLock) {
                if (sPool != null) {
                    holder = sPool;
                    sPool = holder.mNext;
                    holder.mNext = null;
                    holder.mIsPooled = false;
                    sPoolSize--;
                } else {
            ViewLocationHolder holder = sPool.acquire();
            if (holder == null) {
                holder = new ViewLocationHolder();
            }
            holder.init(root, view);
            return holder;
        }
        }

        public void recycle() {
            if (mIsPooled) {
                throw new IllegalStateException("Instance already recycled.");
            }
            clear();
            synchronized (sPoolLock) {
                if (sPoolSize < MAX_POOL_SIZE) {
                    mNext = sPool;
                    mIsPooled = true;
                    sPool = this;
                    sPoolSize++;
                }
            }
            sPool.release(this);
        }

        @Override