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

Commit f4782ec9 authored by Svetoslav Ganov's avatar Svetoslav Ganov
Browse files

Switching the accessibility poolable classes to the new pool management APIs.

The pool management APIs were simplified and using them requires much less
code than implementing your own pooling. Using common pooling logic is
also less error prone. This change swithces AccessibilityEvent and
AccessibilityNodeInfo to the new APIs.

Change-Id: I2dcfe2e1b0b0be5f89bd92359766ae3d6f8a143b
parent abae2a1b
Loading
Loading
Loading
Loading
+6 −27
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package android.view.accessibility;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Pools.SynchronizedPool;

import java.util.ArrayList;
import java.util.List;
@@ -686,11 +687,8 @@ public final class AccessibilityEvent extends AccessibilityRecord implements Par
    public static final int TYPES_ALL_MASK = 0xFFFFFFFF;

    private static final int MAX_POOL_SIZE = 10;
    private static final Object sPoolLock = new Object();
    private static AccessibilityEvent sPool;
    private static int sPoolSize;
    private AccessibilityEvent mNext;
    private boolean mIsInPool;
    private static final SynchronizedPool<AccessibilityEvent> sPool =
            new SynchronizedPool<AccessibilityEvent>(MAX_POOL_SIZE);

    private int mEventType;
    private CharSequence mPackageName;
@@ -916,17 +914,8 @@ public final class AccessibilityEvent extends AccessibilityRecord implements Par
     * @return An instance.
     */
    public static AccessibilityEvent obtain() {
        synchronized (sPoolLock) {
            if (sPool != null) {
                AccessibilityEvent event = sPool;
                sPool = sPool.mNext;
                sPoolSize--;
                event.mNext = null;
                event.mIsInPool = false;
                return event;
            }
            return new AccessibilityEvent();
        }
        AccessibilityEvent event = sPool.acquire();
        return (event != null) ? event : new AccessibilityEvent();
    }

    /**
@@ -939,18 +928,8 @@ public final class AccessibilityEvent extends AccessibilityRecord implements Par
     */
    @Override
    public void recycle() {
        if (mIsInPool) {
            throw new IllegalStateException("Event already recycled!");
        }
        clear();
        synchronized (sPoolLock) {
            if (sPoolSize <= MAX_POOL_SIZE) {
                mNext = sPool;
                sPool = this;
                mIsInPool = true;
                sPoolSize++;
            }
        }
        sPool.release(this);
    }

    /**
+7 −28
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.graphics.Rect;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Pools.SynchronizedPool;
import android.util.SparseLongArray;
import android.view.View;

@@ -354,11 +355,9 @@ public class AccessibilityNodeInfo implements Parcelable {

    // Housekeeping.
    private static final int MAX_POOL_SIZE = 50;
    private static final Object sPoolLock = new Object();
    private static AccessibilityNodeInfo sPool;
    private static int sPoolSize;
    private AccessibilityNodeInfo mNext;
    private boolean mIsInPool;
    private static final SynchronizedPool<AccessibilityNodeInfo> sPool =
            new SynchronizedPool<AccessibilityNodeInfo>(MAX_POOL_SIZE);

    private boolean mSealed;

    // Data.
@@ -1517,17 +1516,8 @@ public class AccessibilityNodeInfo implements Parcelable {
     * @return An instance.
     */
    public static AccessibilityNodeInfo obtain() {
        synchronized (sPoolLock) {
            if (sPool != null) {
                AccessibilityNodeInfo info = sPool;
                sPool = sPool.mNext;
                sPoolSize--;
                info.mNext = null;
                info.mIsInPool = false;
                return info;
            }
            return new AccessibilityNodeInfo();
        }
        AccessibilityNodeInfo info = sPool.acquire();
        return (info != null) ? info : new AccessibilityNodeInfo();
    }

    /**
@@ -1552,18 +1542,8 @@ public class AccessibilityNodeInfo implements Parcelable {
     * @throws IllegalStateException If the info is already recycled.
     */
    public void recycle() {
        if (mIsInPool) {
            throw new IllegalStateException("Info already recycled!");
        }
        clear();
        synchronized (sPoolLock) {
            if (sPoolSize <= MAX_POOL_SIZE) {
                mNext = sPool;
                sPool = this;
                mIsInPool = true;
                sPoolSize++;
            }
        }
        sPool.release(this);
    }

    /**
@@ -1620,7 +1600,6 @@ public class AccessibilityNodeInfo implements Parcelable {
     *
     * @param other The other instance.
     */
    @SuppressWarnings("unchecked")
    private void init(AccessibilityNodeInfo other) {
        mSealed = other.mSealed;
        mSourceNodeId = other.mSourceNodeId;