|
15 | 15 | *******************************************************************************/
|
16 | 16 | package com.nostra13.universalimageloader.core;
|
17 | 17 |
|
| 18 | +import android.annotation.TargetApi; |
| 19 | +import android.app.ActivityManager; |
18 | 20 | import android.content.Context;
|
| 21 | +import android.content.pm.ApplicationInfo; |
| 22 | +import android.os.Build; |
19 | 23 | import com.nostra13.universalimageloader.cache.disc.DiskCache;
|
20 | 24 | import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
|
21 | 25 | import com.nostra13.universalimageloader.cache.disc.impl.ext.LruDiscCache;
|
@@ -107,13 +111,32 @@ private static File createReserveDiskCacheDir(Context context) {
|
107 | 111 | * Creates default implementation of {@link MemoryCache} - {@link LruMemoryCache}<br />
|
108 | 112 | * Default cache size = 1/8 of available app memory.
|
109 | 113 | */
|
110 |
| - public static MemoryCache createMemoryCache(int memoryCacheSize) { |
| 114 | + public static MemoryCache createMemoryCache(Context context, int memoryCacheSize) { |
111 | 115 | if (memoryCacheSize == 0) {
|
112 |
| - memoryCacheSize = (int) (Runtime.getRuntime().maxMemory() / 8); |
| 116 | + ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
| 117 | + int memoryClass = am.getMemoryClass(); |
| 118 | + if (hasHoneycomb() && isLargeHeap(context)) { |
| 119 | + memoryClass = getLargeMemoryClass(am); |
| 120 | + } |
| 121 | + memoryCacheSize = 1024 * 1024 * memoryClass / 8; |
113 | 122 | }
|
114 | 123 | return new LruMemoryCache(memoryCacheSize);
|
115 | 124 | }
|
116 | 125 |
|
| 126 | + private static boolean hasHoneycomb() { |
| 127 | + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB; |
| 128 | + } |
| 129 | + |
| 130 | + @TargetApi(Build.VERSION_CODES.HONEYCOMB) |
| 131 | + private static boolean isLargeHeap(Context context) { |
| 132 | + return (context.getApplicationInfo().flags & ApplicationInfo.FLAG_LARGE_HEAP) != 0; |
| 133 | + } |
| 134 | + |
| 135 | + @TargetApi(Build.VERSION_CODES.HONEYCOMB) |
| 136 | + private static int getLargeMemoryClass(ActivityManager am) { |
| 137 | + return am.getLargeMemoryClass(); |
| 138 | + } |
| 139 | + |
117 | 140 | /** Creates default implementation of {@link ImageDownloader} - {@link BaseImageDownloader} */
|
118 | 141 | public static ImageDownloader createImageDownloader(Context context) {
|
119 | 142 | return new BaseImageDownloader(context);
|
|
0 commit comments