/* * Copyright (c) 2015-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ package com.facebook.imagepipeline.cache; import android.app.ActivityManager; import android.os.Build; import com.facebook.common.internal.Supplier; import com.facebook.common.util.ByteConstants; /** * Supplies {@link MemoryCacheParams} for the bitmap memory cache. */ public class DefaultBitmapMemoryCacheParamsSupplier implements Supplier<MemoryCacheParams> { private static final int MAX_CACHE_ENTRIES = 256; private static final int MAX_EVICTION_QUEUE_SIZE = Integer.MAX_VALUE; private static final int MAX_EVICTION_QUEUE_ENTRIES = Integer.MAX_VALUE; private static final int MAX_CACHE_ENTRY_SIZE = Integer.MAX_VALUE; private final ActivityManager mActivityManager; public DefaultBitmapMemoryCacheParamsSupplier(ActivityManager activityManager) { mActivityManager = activityManager; } @Override public MemoryCacheParams get() { return new MemoryCacheParams( getMaxCacheSize(), MAX_CACHE_ENTRIES, MAX_EVICTION_QUEUE_SIZE, MAX_EVICTION_QUEUE_ENTRIES, MAX_CACHE_ENTRY_SIZE); } private int getMaxCacheSize() { final int maxMemory = Math.min(mActivityManager.getMemoryClass() * ByteConstants.MB, Integer.MAX_VALUE); if (maxMemory < 32 * ByteConstants.MB) { return 4 * ByteConstants.MB; } else if (maxMemory < 64 * ByteConstants.MB) { return 6 * ByteConstants.MB; } else { // We don't want to use more ashmem on Gingerbread for now, since it doesn't respond well to // native memory pressure (doesn't throw exceptions, crashes app, crashes phone) if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD) { return 8 * ByteConstants.MB; } else { return maxMemory / 4; } } } }