1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.internal.widget; 18 19 import android.util.Log; 20 import android.util.Pools; 21 import android.view.View; 22 23 /** 24 * A trivial wrapper around Pools.SynchronizedPool which allows clearing the pool, as well as 25 * disabling the pool class altogether. 26 * @param <T> the type of object in the pool 27 */ 28 public class MessagingPool<T extends View> implements Pools.Pool<T> { 29 private static final boolean ENABLED = false; // disabled to test b/208508846 30 private static final String TAG = "MessagingPool"; 31 private final int mMaxPoolSize; 32 private Pools.SynchronizedPool<T> mCurrentPool; 33 MessagingPool(int maxPoolSize)34 public MessagingPool(int maxPoolSize) { 35 mMaxPoolSize = maxPoolSize; 36 if (ENABLED) { 37 mCurrentPool = new Pools.SynchronizedPool<>(mMaxPoolSize); 38 } 39 } 40 41 @Override acquire()42 public T acquire() { 43 if (!ENABLED) { 44 return null; 45 } 46 T instance = mCurrentPool.acquire(); 47 if (instance.getParent() != null) { 48 Log.wtf(TAG, "acquired " + instance + " with parent " + instance.getParent()); 49 return null; 50 } 51 return instance; 52 } 53 54 @Override release(T instance)55 public boolean release(T instance) { 56 if (instance.getParent() != null) { 57 Log.wtf(TAG, "releasing " + instance + " with parent " + instance.getParent()); 58 return false; 59 } 60 if (!ENABLED) { 61 return false; 62 } 63 return mCurrentPool.release(instance); 64 } 65 66 /** Clear the pool */ clear()67 public void clear() { 68 if (ENABLED) { 69 mCurrentPool = new Pools.SynchronizedPool<>(mMaxPoolSize); 70 } 71 } 72 73 } 74