Browse files

remove monkey project(not used), reformat sources to use 4 spaces ins…

…tead of two
  • Loading branch information...
1 parent 8e4ae6c commit bcd8096b22faedb47173656b5bd91d34a3155fec @luigi-agosti luigi-agosti committed Mar 19, 2012
Showing with 1,595 additions and 1,675 deletions.
  1. +44 −45 core/src/main/java/com/novoda/imageloader/core/ImageManager.java
  2. +50 −41 core/src/main/java/com/novoda/imageloader/core/LoaderContext.java
  3. +143 −143 core/src/main/java/com/novoda/imageloader/core/LoaderSettings.java
  4. +122 −123 core/src/main/java/com/novoda/imageloader/core/bitmap/BitmapUtil.java
  5. +5 −5 core/src/main/java/com/novoda/imageloader/core/cache/CacheManager.java
  6. +26 −27 core/src/main/java/com/novoda/imageloader/core/cache/LruBitmapCache.java
  7. +51 −51 core/src/main/java/com/novoda/imageloader/core/cache/LruCache.java
  8. +18 −19 core/src/main/java/com/novoda/imageloader/core/cache/MapCache.java
  9. +14 −15 core/src/main/java/com/novoda/imageloader/core/cache/NoCache.java
  10. +27 −28 core/src/main/java/com/novoda/imageloader/core/cache/SoftMapCache.java
  11. +7 −7 core/src/main/java/com/novoda/imageloader/core/exception/ImageCopyException.java
  12. +1 −1 core/src/main/java/com/novoda/imageloader/core/exception/ImageNotFoundException.java
  13. +5 −5 core/src/main/java/com/novoda/imageloader/core/exception/MissingSettingException.java
  14. +50 −50 core/src/main/java/com/novoda/imageloader/core/file/BasicFileManager.java
  15. +6 −6 core/src/main/java/com/novoda/imageloader/core/file/FileManager.java
  16. +98 −99 core/src/main/java/com/novoda/imageloader/core/file/util/FileUtil.java
  17. +1 −1 core/src/main/java/com/novoda/imageloader/core/file/util/FlushedInputStream.java
  18. +46 −46 core/src/main/java/com/novoda/imageloader/core/loader/ConcurrentLoader.java
  19. +2 −2 core/src/main/java/com/novoda/imageloader/core/loader/Loader.java
  20. +67 −66 core/src/main/java/com/novoda/imageloader/core/loader/SimpleLoader.java
  21. +19 −19 core/src/main/java/com/novoda/imageloader/core/loader/util/BitmapDisplayer.java
  22. +80 −80 core/src/main/java/com/novoda/imageloader/core/loader/util/LoaderTask.java
  23. +102 −103 core/src/main/java/com/novoda/imageloader/core/loader/util/SingleThreadedLoader.java
  24. +43 −43 core/src/main/java/com/novoda/imageloader/core/model/ImageTag.java
  25. +37 −37 core/src/main/java/com/novoda/imageloader/core/model/ImageTagFactory.java
  26. +73 −73 core/src/main/java/com/novoda/imageloader/core/model/ImageWrapper.java
  27. +1 −1 core/src/main/java/com/novoda/imageloader/core/network/NetworkManager.java
  28. +35 −35 core/src/main/java/com/novoda/imageloader/core/network/UrlNetworkLoader.java
  29. +11 −11 core/src/main/java/com/novoda/imageloader/core/network/UrlUtil.java
  30. +50 −50 core/src/main/java/com/novoda/imageloader/core/service/CacheCleaner.java
  31. +2 −1 core/src/test/java/com/novoda/imageloader/core/Util.java
  32. +105 −107 core/src/test/java/com/novoda/imageloader/core/service/CacheCleanerTest.java
  33. +17 −17 core/src/test/java/com/novoda/imageloader/core/util/UrlUtilTest.java
  34. +27 −27 demo/src/com/novoda/imageloader/demo/DemoApplication.java
  35. +4 −4 demo/src/com/novoda/imageloader/demo/activity/BigImages.java
  36. +73 −71 demo/src/com/novoda/imageloader/demo/activity/base/SingleTableBaseListActivity.java
  37. +40 −36 demo/src/com/novoda/imageloader/demo/provider/CustomUriMatcher.java
  38. +52 −52 demo/src/com/novoda/imageloader/demo/provider/ImageLoaderDemoProvider.java
  39. +41 −41 demo/src/com/novoda/imageloader/demo/provider/SqlFile.java
  40. +0 −38 monkey/pom.xml
  41. +0 −12 monkey/src/main/python/example.py
  42. +0 −16 monkey/src/test/java/com/novoda/lib/imageloader/monkeyrunner/ExampleTest.java
  43. +0 −21 monkey/src/test/java/com/novoda/lib/imageloader/monkeyrunner/MonkeyRunnerUtils.java
View
89 core/src/main/java/com/novoda/imageloader/core/ImageManager.java
@@ -30,52 +30,51 @@
import com.novoda.imageloader.core.network.UrlNetworkLoader;
public class ImageManager {
-
- private LoaderContext loaderContext;
- private Loader loader;
-
- public ImageManager(Context context, LoaderSettings settings) {
- this.loaderContext = new LoaderContext();
- loaderContext.setSettings(settings);
- loaderContext.setFileManager(new BasicFileManager(settings));
- loaderContext.setNetworkManager(new UrlNetworkLoader(settings));
- loaderContext.setResBitmapCache(new SoftMapCache());
- CacheManager cacheManager = settings.getCacheManager();
- if(cacheManager == null) {
- cacheManager = new SoftMapCache();
- }
- loaderContext.setCache(cacheManager);
- if(settings.isUseAsyncTasks()) {
- this.loader = new ConcurrentLoader(loaderContext);
- } else {
- this.loader = new SimpleLoader(loaderContext);
- }
- verifyPermissions(context);
- }
-
- public Loader getLoader() {
- return loader;
- }
- public FileManager getFileManager() {
- return loaderContext.getFileManager();
- }
+ private LoaderContext loaderContext;
+ private Loader loader;
- public NetworkManager getNetworkManager() {
- return loaderContext.getNetworkManager();
- }
+ public ImageManager(Context context, LoaderSettings settings) {
+ this.loaderContext = new LoaderContext();
+ loaderContext.setSettings(settings);
+ loaderContext.setFileManager(new BasicFileManager(settings));
+ loaderContext.setNetworkManager(new UrlNetworkLoader(settings));
+ loaderContext.setResBitmapCache(new SoftMapCache());
+ CacheManager cacheManager = settings.getCacheManager();
+ if (cacheManager == null) {
+ cacheManager = new SoftMapCache();
+ }
+ loaderContext.setCache(cacheManager);
+ if (settings.isUseAsyncTasks()) {
+ this.loader = new ConcurrentLoader(loaderContext);
+ } else {
+ this.loader = new SimpleLoader(loaderContext);
+ }
+ verifyPermissions(context);
+ }
+
+ public Loader getLoader() {
+ return loader;
+ }
+
+ public FileManager getFileManager() {
+ return loaderContext.getFileManager();
+ }
+
+ public NetworkManager getNetworkManager() {
+ return loaderContext.getNetworkManager();
+ }
+
+ private void verifyPermissions(Context context) {
+ verifyPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
+ verifyPermission(context, Manifest.permission.INTERNET);
+ }
+
+ private void verifyPermission(Context c, String permission) {
+ int p = c.getPackageManager().checkPermission(permission, c.getPackageName());
+ if (p == PackageManager.PERMISSION_DENIED) {
+ throw new RuntimeException("ImageLoader : please add the permission " + permission + " to the manifest");
+ }
+ }
- private void verifyPermissions(Context context) {
- verifyPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
- verifyPermission(context, Manifest.permission.INTERNET);
- }
-
- private void verifyPermission(Context c, String permission) {
- int p = c.getPackageManager().checkPermission(permission, c.getPackageName());
- if(p == PackageManager.PERMISSION_DENIED) {
- throw new RuntimeException("ImageLoader : please add the permission "
- + permission + " to the manifest");
- }
- }
-
}
View
91 core/src/main/java/com/novoda/imageloader/core/LoaderContext.java
@@ -21,45 +21,54 @@
import com.novoda.imageloader.core.network.NetworkManager;
public class LoaderContext {
- private FileManager fileManager;
- private NetworkManager networkManager;
- private CacheManager cache;
- private CacheManager resBitmapCache;
- private LoaderSettings settings;
- private BitmapUtil bitmapUtil = new BitmapUtil();
-
- public FileManager getFileManager() {
- return fileManager;
- }
- public void setFileManager(FileManager fileManager) {
- this.fileManager = fileManager;
- }
- public NetworkManager getNetworkManager() {
- return networkManager;
- }
- public void setNetworkManager(NetworkManager networkManager) {
- this.networkManager = networkManager;
- }
- public LoaderSettings getSettings() {
- return settings;
- }
- public void setSettings(LoaderSettings settings) {
- this.settings = settings;
- }
- public CacheManager getResBitmapCache() {
- return resBitmapCache;
- }
- public void setResBitmapCache(CacheManager resBitmapCache) {
- this.resBitmapCache = resBitmapCache;
- }
- public CacheManager getCache() {
- return cache;
- }
- public void setCache(CacheManager cache) {
- this.cache = cache;
- }
-
- public BitmapUtil getBitmapUtil() {
- return bitmapUtil;
- }
+ private FileManager fileManager;
+ private NetworkManager networkManager;
+ private CacheManager cache;
+ private CacheManager resBitmapCache;
+ private LoaderSettings settings;
+ private BitmapUtil bitmapUtil = new BitmapUtil();
+
+ public FileManager getFileManager() {
+ return fileManager;
+ }
+
+ public void setFileManager(FileManager fileManager) {
+ this.fileManager = fileManager;
+ }
+
+ public NetworkManager getNetworkManager() {
+ return networkManager;
+ }
+
+ public void setNetworkManager(NetworkManager networkManager) {
+ this.networkManager = networkManager;
+ }
+
+ public LoaderSettings getSettings() {
+ return settings;
+ }
+
+ public void setSettings(LoaderSettings settings) {
+ this.settings = settings;
+ }
+
+ public CacheManager getResBitmapCache() {
+ return resBitmapCache;
+ }
+
+ public void setResBitmapCache(CacheManager resBitmapCache) {
+ this.resBitmapCache = resBitmapCache;
+ }
+
+ public CacheManager getCache() {
+ return cache;
+ }
+
+ public void setCache(CacheManager cache) {
+ this.cache = cache;
+ }
+
+ public BitmapUtil getBitmapUtil() {
+ return bitmapUtil;
+ }
}
View
286 core/src/main/java/com/novoda/imageloader/core/LoaderSettings.java
@@ -25,148 +25,148 @@
public class LoaderSettings {
- private static final long DEFAULT_EXPIRATION_PERIOD = 7l * 24l * 3600l * 1000l;
- private static final boolean DEFAULT_INCLUDE_QUERY_IN_HASH = true;
- private static final int DEFAULT_CONNECTION_TIMEOUT = 10 * 1000;
- private static final int DEFAULT_READ_TIMEOUT = 10 * 1000;
- private static final boolean DEFAULT_DISCONNECT_ON_EVERY_CALL = false;
- private static final boolean DEFAULT_USE_ASYNC_TASKS = true;
-
- private File cacheDir;
- private int connectionTimeout;
- private int readTimeout;
- private long expirationPeriod;
- private boolean isQueryIncludedInHash;
- private boolean disconnectOnEveryCall;
- private String sdkVersion;
- private CacheManager cacheManager;
- private boolean useAsyncTasks;
-
- public LoaderSettings() {
- this.setExpirationPeriod(DEFAULT_EXPIRATION_PERIOD);
- this.setQueryIncludedInHash(DEFAULT_INCLUDE_QUERY_IN_HASH);
- this.setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
- this.setReadTimeout(DEFAULT_READ_TIMEOUT);
- this.setDisconnectOnEveryCall(DEFAULT_DISCONNECT_ON_EVERY_CALL);
- this.setUseAsyncTasks(DEFAULT_USE_ASYNC_TASKS);
- }
-
- public File getCacheDir() {
- return cacheDir;
- }
-
- public void setCacheDir(File cacheDir) {
- this.cacheDir = cacheDir;
- }
-
- public long getExpirationPeriod() {
- return expirationPeriod;
- }
-
- public void setExpirationPeriod(long expirationPeriod) {
- this.expirationPeriod = expirationPeriod;
- }
-
- public boolean isQueryIncludedInHash() {
- return isQueryIncludedInHash;
- }
-
- public void setQueryIncludedInHash(boolean isQueryIncludedInHash) {
- this.isQueryIncludedInHash = isQueryIncludedInHash;
- }
-
- public int getConnectionTimeout() {
- return connectionTimeout;
- }
-
- public void setConnectionTimeout(int connectionTimeout) {
- this.connectionTimeout = connectionTimeout;
- }
-
- public int getReadTimeout() {
- return readTimeout;
- }
-
- public void setReadTimeout(int readTimeout) {
- this.readTimeout = readTimeout;
- }
-
- public boolean getDisconnectOnEveryCall() {
- return disconnectOnEveryCall;
- }
-
- public void setDisconnectOnEveryCall(boolean disconnectOnEveryCall) {
- this.disconnectOnEveryCall = disconnectOnEveryCall;
- }
-
- public void setSdkVersion(String sdkVersion) {
- this.sdkVersion = sdkVersion;
- }
-
- public String getSdkVersion() {
- return this.sdkVersion;
- }
-
- public CacheManager getCacheManager() {
- return cacheManager;
- }
-
- public void setCacheManager(CacheManager cacheManager) {
- this.cacheManager = cacheManager;
- }
-
- public boolean isUseAsyncTasks() {
- return useAsyncTasks;
- }
-
- public void setUseAsyncTasks(boolean useAsyncTasks) {
- this.useAsyncTasks = useAsyncTasks;
- }
-
- public static class SettingsBuilder {
-
- private LoaderSettings settings;
-
- public SettingsBuilder() {
- settings = new LoaderSettings();
- }
-
- public SettingsBuilder withEnableQueryInHashGeneration(boolean enableQueryInHashGeneration) {
- settings.setQueryIncludedInHash(enableQueryInHashGeneration);
- return this;
- }
-
- public SettingsBuilder withConnectionTimeout(int connectionTimeout) {
- settings.setConnectionTimeout(connectionTimeout);
- return this;
- }
-
- public SettingsBuilder withReadTimeout(int readTimeout) {
- settings.setReadTimeout(readTimeout);
- return this;
- }
-
- public SettingsBuilder withDisconnectOnEveryCall(boolean disconnectOnEveryCall) {
- settings.setDisconnectOnEveryCall(disconnectOnEveryCall);
- return this;
- }
-
- public SettingsBuilder withCacheManager(CacheManager cacheManager) {
- settings.setCacheManager(cacheManager);
- return this;
- }
-
- public SettingsBuilder withAsyncTasks(boolean useAsyncTasks) {
- settings.setUseAsyncTasks(useAsyncTasks);
- return this;
- }
-
- public LoaderSettings build(Context context){
- settings.setCacheDir(new FileUtil().prepareCacheDirectory(context));
- settings.setSdkVersion(Build.VERSION.SDK);
- return settings;
- }
-
- }
+ private static final long DEFAULT_EXPIRATION_PERIOD = 7l * 24l * 3600l * 1000l;
+ private static final boolean DEFAULT_INCLUDE_QUERY_IN_HASH = true;
+ private static final int DEFAULT_CONNECTION_TIMEOUT = 10 * 1000;
+ private static final int DEFAULT_READ_TIMEOUT = 10 * 1000;
+ private static final boolean DEFAULT_DISCONNECT_ON_EVERY_CALL = false;
+ private static final boolean DEFAULT_USE_ASYNC_TASKS = true;
+
+ private File cacheDir;
+ private int connectionTimeout;
+ private int readTimeout;
+ private long expirationPeriod;
+ private boolean isQueryIncludedInHash;
+ private boolean disconnectOnEveryCall;
+ private String sdkVersion;
+ private CacheManager cacheManager;
+ private boolean useAsyncTasks;
+
+ public LoaderSettings() {
+ this.setExpirationPeriod(DEFAULT_EXPIRATION_PERIOD);
+ this.setQueryIncludedInHash(DEFAULT_INCLUDE_QUERY_IN_HASH);
+ this.setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
+ this.setReadTimeout(DEFAULT_READ_TIMEOUT);
+ this.setDisconnectOnEveryCall(DEFAULT_DISCONNECT_ON_EVERY_CALL);
+ this.setUseAsyncTasks(DEFAULT_USE_ASYNC_TASKS);
+ }
+
+ public File getCacheDir() {
+ return cacheDir;
+ }
+
+ public void setCacheDir(File cacheDir) {
+ this.cacheDir = cacheDir;
+ }
+
+ public long getExpirationPeriod() {
+ return expirationPeriod;
+ }
+
+ public void setExpirationPeriod(long expirationPeriod) {
+ this.expirationPeriod = expirationPeriod;
+ }
+
+ public boolean isQueryIncludedInHash() {
+ return isQueryIncludedInHash;
+ }
+
+ public void setQueryIncludedInHash(boolean isQueryIncludedInHash) {
+ this.isQueryIncludedInHash = isQueryIncludedInHash;
+ }
+
+ public int getConnectionTimeout() {
+ return connectionTimeout;
+ }
+
+ public void setConnectionTimeout(int connectionTimeout) {
+ this.connectionTimeout = connectionTimeout;
+ }
+
+ public int getReadTimeout() {
+ return readTimeout;
+ }
+
+ public void setReadTimeout(int readTimeout) {
+ this.readTimeout = readTimeout;
+ }
+
+ public boolean getDisconnectOnEveryCall() {
+ return disconnectOnEveryCall;
+ }
+
+ public void setDisconnectOnEveryCall(boolean disconnectOnEveryCall) {
+ this.disconnectOnEveryCall = disconnectOnEveryCall;
+ }
+
+ public void setSdkVersion(String sdkVersion) {
+ this.sdkVersion = sdkVersion;
+ }
+
+ public String getSdkVersion() {
+ return this.sdkVersion;
+ }
+
+ public CacheManager getCacheManager() {
+ return cacheManager;
+ }
+
+ public void setCacheManager(CacheManager cacheManager) {
+ this.cacheManager = cacheManager;
+ }
+
+ public boolean isUseAsyncTasks() {
+ return useAsyncTasks;
+ }
+
+ public void setUseAsyncTasks(boolean useAsyncTasks) {
+ this.useAsyncTasks = useAsyncTasks;
+ }
+
+ public static class SettingsBuilder {
+
+ private LoaderSettings settings;
+
+ public SettingsBuilder() {
+ settings = new LoaderSettings();
+ }
+
+ public SettingsBuilder withEnableQueryInHashGeneration(boolean enableQueryInHashGeneration) {
+ settings.setQueryIncludedInHash(enableQueryInHashGeneration);
+ return this;
+ }
+
+ public SettingsBuilder withConnectionTimeout(int connectionTimeout) {
+ settings.setConnectionTimeout(connectionTimeout);
+ return this;
+ }
+
+ public SettingsBuilder withReadTimeout(int readTimeout) {
+ settings.setReadTimeout(readTimeout);
+ return this;
+ }
+
+ public SettingsBuilder withDisconnectOnEveryCall(boolean disconnectOnEveryCall) {
+ settings.setDisconnectOnEveryCall(disconnectOnEveryCall);
+ return this;
+ }
+
+ public SettingsBuilder withCacheManager(CacheManager cacheManager) {
+ settings.setCacheManager(cacheManager);
+ return this;
+ }
+
+ public SettingsBuilder withAsyncTasks(boolean useAsyncTasks) {
+ settings.setUseAsyncTasks(useAsyncTasks);
+ return this;
+ }
+
+ public LoaderSettings build(Context context) {
+ settings.setCacheDir(new FileUtil().prepareCacheDirectory(context));
+ settings.setSdkVersion(Build.VERSION.SDK);
+ return settings;
+ }
+
+ }
}
View
245 core/src/main/java/com/novoda/imageloader/core/bitmap/BitmapUtil.java
@@ -27,129 +27,128 @@
public class BitmapUtil {
- private static final int BUFFER_SIZE = 64 * 1024;
-
- public Bitmap decodeFileAndScale(File f, int width, int height) {
- updateLastModifiedForCache(f);
- int suggestedSize = height;
- if (width > height) {
- suggestedSize = height;
- }
- Bitmap unscaledBitmap = decodeFile(f, suggestedSize);
- if(unscaledBitmap == null) {
- return null;
- }
- return scaleBitmap(unscaledBitmap, width, height);
- }
-
- public Bitmap scaleResourceBitmap(ImageWrapper iw, int resourceId) {
- Context c = iw.getContext();
- Bitmap b = null;
- try {
- b = BitmapFactory.decodeResource(c.getResources(), resourceId);
- return scaleBitmap(b, iw.getWidth(), iw.getHeight());
- } catch (final Throwable e) {
- System.gc();
- }
- return null;
- }
-
- public Bitmap scaleBitmap(Bitmap b, int width, int height) {
- int imageHeight = b.getHeight();
- int imageWidth = b.getWidth();
- int finalWidth = width;
- int finalHeight = height;
- if (imageHeight > imageWidth) {
- float factor = ((float) height) / ((float) imageHeight);
- finalHeight = new Float(imageHeight * factor).intValue();
- finalWidth = new Float(imageWidth * factor).intValue();
- } else {
- float factor = ((float) width) / ((float) imageWidth);
- finalHeight = new Float(imageHeight * factor).intValue();
- finalWidth = new Float(imageWidth * factor).intValue();
+ private static final int BUFFER_SIZE = 64 * 1024;
+
+ public Bitmap decodeFileAndScale(File f, int width, int height) {
+ updateLastModifiedForCache(f);
+ int suggestedSize = height;
+ if (width > height) {
+ suggestedSize = height;
+ }
+ Bitmap unscaledBitmap = decodeFile(f, suggestedSize);
+ if (unscaledBitmap == null) {
+ return null;
+ }
+ return scaleBitmap(unscaledBitmap, width, height);
+ }
+
+ public Bitmap scaleResourceBitmap(ImageWrapper iw, int resourceId) {
+ Context c = iw.getContext();
+ Bitmap b = null;
+ try {
+ b = BitmapFactory.decodeResource(c.getResources(), resourceId);
+ return scaleBitmap(b, iw.getWidth(), iw.getHeight());
+ } catch (final Throwable e) {
+ System.gc();
+ }
+ return null;
+ }
+
+ public Bitmap scaleBitmap(Bitmap b, int width, int height) {
+ int imageHeight = b.getHeight();
+ int imageWidth = b.getWidth();
+ int finalWidth = width;
+ int finalHeight = height;
+ if (imageHeight > imageWidth) {
+ float factor = ((float) height) / ((float) imageHeight);
+ finalHeight = new Float(imageHeight * factor).intValue();
+ finalWidth = new Float(imageWidth * factor).intValue();
+ } else {
+ float factor = ((float) width) / ((float) imageWidth);
+ finalHeight = new Float(imageHeight * factor).intValue();
+ finalWidth = new Float(imageWidth * factor).intValue();
+ }
+ Bitmap scaled = null;
+ try {
+ scaled = Bitmap.createScaledBitmap(b, finalWidth, finalHeight, true);
+ } catch (final Throwable e) {
+ System.gc();
+ }
+ recycle(b);
+ return scaled;
}
- Bitmap scaled = null;
- try {
- scaled = Bitmap.createScaledBitmap(b, finalWidth, finalHeight, true);
- } catch (final Throwable e) {
- System.gc();
- }
- recycle(b);
- return scaled;
- }
-
- private void recycle(Bitmap scaled) {
- try {
- scaled.recycle();
- } catch (Exception e) {
- //
- }
- }
-
- private void updateLastModifiedForCache(File f) {
- f.setLastModified(System.currentTimeMillis());
- }
-
- private Bitmap decodeFile(File f, int suggestedSize) {
- Bitmap bitmap = null;
- FileInputStream fis = null;
- try {
- int scale = evaluateScale(f, suggestedSize);
- final BitmapFactory.Options options = new BitmapFactory.Options();
- options.inSampleSize = scale;
- options.inTempStorage = new byte[BUFFER_SIZE];
- options.inPurgeable = true;
- fis = new FileInputStream(f);
- bitmap = BitmapFactory.decodeStream(fis, null, options);
- } catch (final Throwable e) {
- System.gc();
- } finally {
- closeSilently(fis);
- }
- return bitmap;
- }
-
- private int evaluateScale(File f, int suggestedSize) {
- final BitmapFactory.Options o = new BitmapFactory.Options();
- o.inJustDecodeBounds = true;
- decodeFileToPopulateOptions(f, o);
- return calculateScale(suggestedSize, o.outWidth, o.outHeight);
- }
-
- private void decodeFileToPopulateOptions(File f, final BitmapFactory.Options o) {
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(f);
- BitmapFactory.decodeStream(fis, null, o);
- closeSilently(fis);
- } catch (final Throwable e) {
- System.gc();
- } finally {
- closeSilently(fis);
- }
- }
-
- private void closeSilently(Closeable c) {
- try {
- if (c != null) {
- c.close();
- }
- } catch (Exception e) {
+
+ private void recycle(Bitmap scaled) {
+ try {
+ scaled.recycle();
+ } catch (Exception e) {
+ //
+ }
+ }
+
+ private void updateLastModifiedForCache(File f) {
+ f.setLastModified(System.currentTimeMillis());
+ }
+
+ private Bitmap decodeFile(File f, int suggestedSize) {
+ Bitmap bitmap = null;
+ FileInputStream fis = null;
+ try {
+ int scale = evaluateScale(f, suggestedSize);
+ final BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inSampleSize = scale;
+ options.inTempStorage = new byte[BUFFER_SIZE];
+ options.inPurgeable = true;
+ fis = new FileInputStream(f);
+ bitmap = BitmapFactory.decodeStream(fis, null, options);
+ } catch (final Throwable e) {
+ System.gc();
+ } finally {
+ closeSilently(fis);
+ }
+ return bitmap;
}
- }
-
- private int calculateScale(final int requiredSize, int widthTmp, int heightTmp) {
- int scale = 1;
- while (true) {
- if ((widthTmp / 2) < requiredSize
- || (heightTmp / 2) < requiredSize) {
- break;
- }
- widthTmp /= 2;
- heightTmp /= 2;
- scale *= 2;
- }
- return scale;
- }
-
+
+ private int evaluateScale(File f, int suggestedSize) {
+ final BitmapFactory.Options o = new BitmapFactory.Options();
+ o.inJustDecodeBounds = true;
+ decodeFileToPopulateOptions(f, o);
+ return calculateScale(suggestedSize, o.outWidth, o.outHeight);
+ }
+
+ private void decodeFileToPopulateOptions(File f, final BitmapFactory.Options o) {
+ FileInputStream fis = null;
+ try {
+ fis = new FileInputStream(f);
+ BitmapFactory.decodeStream(fis, null, o);
+ closeSilently(fis);
+ } catch (final Throwable e) {
+ System.gc();
+ } finally {
+ closeSilently(fis);
+ }
+ }
+
+ private void closeSilently(Closeable c) {
+ try {
+ if (c != null) {
+ c.close();
+ }
+ } catch (Exception e) {
+ }
+ }
+
+ private int calculateScale(final int requiredSize, int widthTmp, int heightTmp) {
+ int scale = 1;
+ while (true) {
+ if ((widthTmp / 2) < requiredSize || (heightTmp / 2) < requiredSize) {
+ break;
+ }
+ widthTmp /= 2;
+ heightTmp /= 2;
+ scale *= 2;
+ }
+ return scale;
+ }
+
}
View
10 core/src/main/java/com/novoda/imageloader/core/cache/CacheManager.java
@@ -19,12 +19,12 @@
public interface CacheManager {
- boolean hasBitmap(String url);
+ boolean hasBitmap(String url);
- Bitmap get(String url);
+ Bitmap get(String url);
- void put(String url, Bitmap bmp);
+ void put(String url, Bitmap bmp);
+
+ void clean();
- void clean();
-
}
View
53 core/src/main/java/com/novoda/imageloader/core/cache/LruBitmapCache.java
@@ -17,33 +17,32 @@
import android.graphics.Bitmap;
-
public class LruBitmapCache implements CacheManager {
- private LruCache<String, Bitmap> cache;
-
- public LruBitmapCache(int capacity) {
- cache = new LruCache<String, Bitmap>(capacity);
- }
-
- @Override
- public boolean hasBitmap(String url) {
- return cache.hasKey(url);
- }
-
- @Override
- public Bitmap get(String url) {
- return cache.get(url);
- }
-
- @Override
- public void put(String url, Bitmap bmp) {
- cache.put(url, bmp);
- }
-
- @Override
- public void clean() {
- cache.clear();
- }
-
+ private LruCache<String, Bitmap> cache;
+
+ public LruBitmapCache(int capacity) {
+ cache = new LruCache<String, Bitmap>(capacity);
+ }
+
+ @Override
+ public boolean hasBitmap(String url) {
+ return cache.hasKey(url);
+ }
+
+ @Override
+ public Bitmap get(String url) {
+ return cache.get(url);
+ }
+
+ @Override
+ public void put(String url, Bitmap bmp) {
+ cache.put(url, bmp);
+ }
+
+ @Override
+ public void clean() {
+ cache.clear();
+ }
+
}
View
102 core/src/main/java/com/novoda/imageloader/core/cache/LruCache.java
@@ -22,62 +22,62 @@
import java.util.Map;
public class LruCache<K, V> {
-
- private final HashMap<K, V> mLruMap;
- private final HashMap<K, Entry<K, V>> mWeakMap = new HashMap<K, Entry<K, V>>();
- private ReferenceQueue<V> mQueue = new ReferenceQueue<V>();
- @SuppressWarnings("serial")
- public LruCache(final int capacity) {
- mLruMap = new LinkedHashMap<K, V>(16, 0.75f, true) {
- @Override
- protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
- return size() > capacity;
- }
- };
- }
+ private final HashMap<K, V> mLruMap;
+ private final HashMap<K, Entry<K, V>> mWeakMap = new HashMap<K, Entry<K, V>>();
+ private ReferenceQueue<V> mQueue = new ReferenceQueue<V>();
- private static class Entry<K, V> extends WeakReference<V> {
- K mKey;
+ @SuppressWarnings("serial")
+ public LruCache(final int capacity) {
+ mLruMap = new LinkedHashMap<K, V>(16, 0.75f, true) {
+ @Override
+ protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
+ return size() > capacity;
+ }
+ };
+ }
- public Entry(K key, V value, ReferenceQueue<V> queue) {
- super(value, queue);
- mKey = key;
- }
- }
+ private static class Entry<K, V> extends WeakReference<V> {
+ K mKey;
- @SuppressWarnings("unchecked")
- private void cleanUpWeakMap() {
- Entry<K, V> entry = (Entry<K, V>) mQueue.poll();
- while (entry != null) {
- mWeakMap.remove(entry.mKey);
- entry = (Entry<K, V>) mQueue.poll();
- }
- }
+ public Entry(K key, V value, ReferenceQueue<V> queue) {
+ super(value, queue);
+ mKey = key;
+ }
+ }
- public synchronized V put(K key, V value) {
- cleanUpWeakMap();
- mLruMap.put(key, value);
- Entry<K, V> entry = mWeakMap.put(
- key, new Entry<K, V>(key, value, mQueue));
- return entry == null ? null : entry.get();
- }
+ @SuppressWarnings("unchecked")
+ private void cleanUpWeakMap() {
+ Entry<K, V> entry = (Entry<K, V>) mQueue.poll();
+ while (entry != null) {
+ mWeakMap.remove(entry.mKey);
+ entry = (Entry<K, V>) mQueue.poll();
+ }
+ }
- public synchronized V get(K key) {
- cleanUpWeakMap();
- V value = mLruMap.get(key);
- if (value != null) return value;
- Entry<K, V> entry = mWeakMap.get(key);
- return entry == null ? null : entry.get();
- }
-
- public synchronized boolean hasKey(K key) {
- return mWeakMap.containsKey(key);
-}
+ public synchronized V put(K key, V value) {
+ cleanUpWeakMap();
+ mLruMap.put(key, value);
+ Entry<K, V> entry = mWeakMap.put(key, new Entry<K, V>(key, value, mQueue));
+ return entry == null ? null : entry.get();
+ }
+
+ public synchronized V get(K key) {
+ cleanUpWeakMap();
+ V value = mLruMap.get(key);
+ if (value != null)
+ return value;
+ Entry<K, V> entry = mWeakMap.get(key);
+ return entry == null ? null : entry.get();
+ }
+
+ public synchronized boolean hasKey(K key) {
+ return mWeakMap.containsKey(key);
+ }
- public synchronized void clear() {
- mLruMap.clear();
- mWeakMap.clear();
- mQueue = new ReferenceQueue<V>();
- }
+ public synchronized void clear() {
+ mLruMap.clear();
+ mWeakMap.clear();
+ mQueue = new ReferenceQueue<V>();
+ }
}
View
37 core/src/main/java/com/novoda/imageloader/core/cache/MapCache.java
@@ -17,31 +17,30 @@
import java.util.HashMap;
-
import android.graphics.Bitmap;
public class MapCache implements CacheManager {
- private HashMap<String, Bitmap> cache = new HashMap<String, Bitmap>();
+ private HashMap<String, Bitmap> cache = new HashMap<String, Bitmap>();
+
+ @Override
+ public boolean hasBitmap(String url) {
+ return cache.containsKey(url);
+ }
- @Override
- public boolean hasBitmap(String url) {
- return cache.containsKey(url);
- }
+ @Override
+ public Bitmap get(String url) {
+ return cache.get(url);
+ }
- @Override
- public Bitmap get(String url) {
- return cache.get(url);
- }
+ @Override
+ public void put(String url, Bitmap bmp) {
+ cache.put(url, bmp);
+ }
- @Override
- public void put(String url, Bitmap bmp) {
- cache.put(url, bmp);
- }
+ @Override
+ public void clean() {
+ cache.clear();
+ }
- @Override
- public void clean() {
- cache.clear();
- }
-
}
View
29 core/src/main/java/com/novoda/imageloader/core/cache/NoCache.java
@@ -15,27 +15,26 @@
*/
package com.novoda.imageloader.core.cache;
-
import android.graphics.Bitmap;
public class NoCache implements CacheManager {
- @Override
- public boolean hasBitmap(String url) {
- return false;
- }
+ @Override
+ public boolean hasBitmap(String url) {
+ return false;
+ }
- @Override
- public Bitmap get(String url) {
- return null;
- }
+ @Override
+ public Bitmap get(String url) {
+ return null;
+ }
- @Override
- public void put(String url, Bitmap bmp) {
- }
+ @Override
+ public void put(String url, Bitmap bmp) {
+ }
- @Override
- public void clean() {
- }
+ @Override
+ public void clean() {
+ }
}
View
55 core/src/main/java/com/novoda/imageloader/core/cache/SoftMapCache.java
@@ -18,41 +18,40 @@
import java.lang.ref.SoftReference;
import java.util.HashMap;
-
import android.graphics.Bitmap;
public class SoftMapCache implements CacheManager {
- private HashMap<String, SoftReference<Bitmap>> cache = new HashMap<String, SoftReference<Bitmap>>();
+ private HashMap<String, SoftReference<Bitmap>> cache = new HashMap<String, SoftReference<Bitmap>>();
+
+ @Override
+ public boolean hasBitmap(String url) {
+ if (!cache.containsKey(url)) {
+ return false;
+ }
+ if (cache.get(url) == null) {
+ return false;
+ }
+ return true;
+ }
- @Override
- public boolean hasBitmap(String url) {
- if (!cache.containsKey(url)) {
- return false;
+ @Override
+ public Bitmap get(String url) {
+ SoftReference<Bitmap> bmpr = cache.get(url);
+ if (bmpr == null) {
+ return null;
+ }
+ return bmpr.get();
}
- if (cache.get(url) == null) {
- return false;
+
+ @Override
+ public void put(String url, Bitmap bmp) {
+ cache.put(url, new SoftReference<Bitmap>(bmp));
}
- return true;
- }
-
- @Override
- public Bitmap get(String url) {
- SoftReference<Bitmap> bmpr = cache.get(url);
- if (bmpr == null) {
- return null;
+
+ @Override
+ public void clean() {
+ cache.clear();
}
- return bmpr.get();
- }
-
- @Override
- public void put(String url, Bitmap bmp) {
- cache.put(url, new SoftReference<Bitmap>(bmp));
- }
-
- @Override
- public void clean() {
- cache.clear();
- }
}
View
14 core/src/main/java/com/novoda/imageloader/core/exception/ImageCopyException.java
@@ -17,14 +17,14 @@
public class ImageCopyException extends Exception {
- private static final long serialVersionUID = 1L;
+ private static final long serialVersionUID = 1L;
- public ImageCopyException(String message) {
- super(message);
- }
+ public ImageCopyException(String message) {
+ super(message);
+ }
- public ImageCopyException(Throwable cause) {
- super(cause);
- }
+ public ImageCopyException(Throwable cause) {
+ super(cause);
+ }
}
View
2 core/src/main/java/com/novoda/imageloader/core/exception/ImageNotFoundException.java
@@ -17,6 +17,6 @@
public class ImageNotFoundException extends RuntimeException {
- private static final long serialVersionUID = 1L;
+ private static final long serialVersionUID = 1L;
}
View
10 core/src/main/java/com/novoda/imageloader/core/exception/MissingSettingException.java
@@ -17,10 +17,10 @@
public class MissingSettingException extends RuntimeException {
- private static final long serialVersionUID = 1L;
+ private static final long serialVersionUID = 1L;
+
+ public MissingSettingException(String message) {
+ super(message);
+ }
- public MissingSettingException(String message) {
- super(message);
- }
-
}
View
100 core/src/main/java/com/novoda/imageloader/core/file/BasicFileManager.java
@@ -26,57 +26,57 @@
public class BasicFileManager implements FileManager {
- private LoaderSettings settings;
-
- public BasicFileManager(LoaderSettings settings) {
- this.settings = settings;
- }
-
- @Override
- public void delete(Context context) {
- sendCacheCleanUpBroadcast(context, 0);
- }
-
- @Override
- public void clean(Context context) {
- long expirationPeriod = settings.getExpirationPeriod();
- sendCacheCleanUpBroadcast(context, expirationPeriod);
- }
+ private LoaderSettings settings;
- @Override
- public String getFilePath(String imageUrl) {
- File f = getFile(imageUrl);
- if (f.exists()) {
- return f.getAbsolutePath();
- }
- return null;
- }
+ public BasicFileManager(LoaderSettings settings) {
+ this.settings = settings;
+ }
- private void sendCacheCleanUpBroadcast(Context context, long expirationPeriod) {
- String path = settings.getCacheDir().getAbsolutePath();
- Intent i = CacheCleaner.getCleanCacheIntent(path, expirationPeriod);
- i.setPackage(context.getPackageName());
- context.startService(i);
- }
+ @Override
+ public void delete(Context context) {
+ sendCacheCleanUpBroadcast(context, 0);
+ }
+
+ @Override
+ public void clean(Context context) {
+ long expirationPeriod = settings.getExpirationPeriod();
+ sendCacheCleanUpBroadcast(context, expirationPeriod);
+ }
+
+ @Override
+ public String getFilePath(String imageUrl) {
+ File f = getFile(imageUrl);
+ if (f.exists()) {
+ return f.getAbsolutePath();
+ }
+ return null;
+ }
+
+ private void sendCacheCleanUpBroadcast(Context context, long expirationPeriod) {
+ String path = settings.getCacheDir().getAbsolutePath();
+ Intent i = CacheCleaner.getCleanCacheIntent(path, expirationPeriod);
+ i.setPackage(context.getPackageName());
+ context.startService(i);
+ }
+
+ @Override
+ public boolean exists(String path) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public File getFile(String url) {
+ url = processUrl(url);
+ String filename = String.valueOf(url.hashCode());
+ return new File(settings.getCacheDir(), filename);
+ }
+
+ private String processUrl(String url) {
+ if (!settings.isQueryIncludedInHash()) {
+ return url;
+ }
+ return new UrlUtil().removeQuery(url);
+ }
- @Override
- public boolean exists(String path) {
- // TODO Auto-generated method stub
- return false;
- }
-
- @Override
- public File getFile(String url) {
- url = processUrl(url);
- String filename = String.valueOf(url.hashCode());
- return new File(settings.getCacheDir(), filename);
- }
-
- private String processUrl(String url) {
- if (!settings.isQueryIncludedInHash()) {
- return url;
- }
- return new UrlUtil().removeQuery(url);
- }
-
}
View
12 core/src/main/java/com/novoda/imageloader/core/file/FileManager.java
@@ -21,14 +21,14 @@
public interface FileManager {
- void delete(Context context);
+ void delete(Context context);
- void clean(Context context);
+ void clean(Context context);
- String getFilePath(String url);
-
- boolean exists(String path);
+ String getFilePath(String url);
- File getFile(String url);
+ boolean exists(String path);
+
+ File getFile(String url);
}
View
197 core/src/main/java/com/novoda/imageloader/core/file/util/FileUtil.java
@@ -30,114 +30,113 @@
public class FileUtil {
- private static final String NOMEDIA_FILE_NAME = ".nomedia";
- private static final String DEFAULT_IMAGE_FOLDER_NAME = "/imagedata";
- private static final String TAG = "ImageLoader";
-
- private static final int BUFFER_SIZE = 60*1024;
- private static final byte[] BUFFER = new byte[BUFFER_SIZE];
-
- public void closeSilently(Closeable c) {
- try {
- if (c != null) {
- c.close();
- }
- } catch (Exception e) {
- Log.e(TAG, "Problem closing stream " + e.getMessage());
+ private static final String NOMEDIA_FILE_NAME = ".nomedia";
+ private static final String DEFAULT_IMAGE_FOLDER_NAME = "/imagedata";
+ private static final String TAG = "ImageLoader";
+
+ private static final int BUFFER_SIZE = 60 * 1024;
+ private static final byte[] BUFFER = new byte[BUFFER_SIZE];
+
+ public void closeSilently(Closeable c) {
+ try {
+ if (c != null) {
+ c.close();
+ }
+ } catch (Exception e) {
+ Log.e(TAG, "Problem closing stream " + e.getMessage());
+ }
}
- }
-
- public void copyStream(InputStream is, OutputStream os) {
- try {
- while (true) {
- synchronized (BUFFER) {
- int amountRead = is.read(BUFFER);
- if (amountRead == -1) {
- break;
- }
- os.write(BUFFER, 0, amountRead);
+
+ public void copyStream(InputStream is, OutputStream os) {
+ try {
+ while (true) {
+ synchronized (BUFFER) {
+ int amountRead = is.read(BUFFER);
+ if (amountRead == -1) {
+ break;
+ }
+ os.write(BUFFER, 0, amountRead);
+ }
+ }
+ } catch (Exception ex) {
+ Log.e(TAG, "Exception : ", ex);
}
- }
- } catch (Exception ex) {
- Log.e(TAG, "Exception : ", ex);
}
- }
-
- public boolean deleteFileCache(String cacheDirFullPath) {
- return reduceFileCache(cacheDirFullPath, -1);
- }
-
- public boolean reduceFileCache(String cacheDirFullPath, long expirationPeriod) {
- File cacheDir = new File(cacheDirFullPath);
- if (cacheDir.isDirectory()) {
- File[] children = cacheDir.listFiles();
- long lastModifiedThreashold = System.currentTimeMillis() - expirationPeriod;
- for (File f : children) {
- if (f.lastModified() < lastModifiedThreashold) {
- f.delete();
+
+ public boolean deleteFileCache(String cacheDirFullPath) {
+ return reduceFileCache(cacheDirFullPath, -1);
+ }
+
+ public boolean reduceFileCache(String cacheDirFullPath, long expirationPeriod) {
+ File cacheDir = new File(cacheDirFullPath);
+ if (cacheDir.isDirectory()) {
+ File[] children = cacheDir.listFiles();
+ long lastModifiedThreashold = System.currentTimeMillis() - expirationPeriod;
+ for (File f : children) {
+ if (f.lastModified() < lastModifiedThreashold) {
+ f.delete();
+ }
+ }
}
- }
+ return true;
}
- return true;
- }
-
- public void copy(File src, File dst) throws ImageCopyException {
- InputStream in = null;
- OutputStream out = null;
- try {
- in = new FileInputStream(src);
- out = new FileOutputStream(dst);
- byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) > 0) {
- out.write(buf, 0, len);
- }
- } catch (IOException e) {
- throw new ImageCopyException(e);
- } finally {
- closeSilently(out);
- closeSilently(in);
+
+ public void copy(File src, File dst) throws ImageCopyException {
@charroch
Novoda member

Using the file nio will speed this up 🚤

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ InputStream in = null;
+ OutputStream out = null;
+ try {
+ in = new FileInputStream(src);
+ out = new FileOutputStream(dst);
+ byte[] buf = new byte[1024];
+ int len;
+ while ((len = in.read(buf)) > 0) {
+ out.write(buf, 0, len);
+ }
+ } catch (IOException e) {
+ throw new ImageCopyException(e);
+ } finally {
+ closeSilently(out);
+ closeSilently(in);
+ }
}
- }
-
- public File prepareCacheDirectory(Context context) {
- File dir = null;
- if(!isMounted()) {
- dir = preparePhoneCacheDir(context);
- } else {
- dir = prepareExternalCacheDir(context);
+
+ public File prepareCacheDirectory(Context context) {
+ File dir = null;
+ if (!isMounted()) {
+ dir = preparePhoneCacheDir(context);
+ } else {
+ dir = prepareExternalCacheDir(context);
+ }
+ addNomediaFile(dir);
+ return dir;
}
- addNomediaFile(dir);
- return dir;
- }
-
- public boolean isMounted() {
- if (android.os.Environment.getExternalStorageState().equals(
- android.os.Environment.MEDIA_MOUNTED)) {
- return true;
+
+ public boolean isMounted() {
+ if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
+ return true;
+ }
+ return false;
}
- return false;
- }
-
- private File prepareExternalCacheDir(Context context) {
- String relativepath = context.getPackageName() + DEFAULT_IMAGE_FOLDER_NAME;
- File file = new File(android.os.Environment.getExternalStorageDirectory(), relativepath);
- if (!file.isDirectory()) {
- file.mkdirs();
+
+ private File prepareExternalCacheDir(Context context) {
+ String relativepath = context.getPackageName() + DEFAULT_IMAGE_FOLDER_NAME;
+ File file = new File(android.os.Environment.getExternalStorageDirectory(), relativepath);
+ if (!file.isDirectory()) {
+ file.mkdirs();
+ }
+ return file;
}
- return file;
- }
-
- private File preparePhoneCacheDir(Context context) {
- return context.getCacheDir();
- }
-
- private void addNomediaFile(File dir) {
- try {
- new File(dir, NOMEDIA_FILE_NAME).createNewFile();
- } catch(Exception e) {
- //Don't care if doesn't save the file
+
+ private File preparePhoneCacheDir(Context context) {
+ return context.getCacheDir();
+ }
+
+ private void addNomediaFile(File dir) {
+ try {
+ new File(dir, NOMEDIA_FILE_NAME).createNewFile();
+ } catch (Exception e) {
+ // Don't care if doesn't save the file
+ }
}
- }
}
View
2 core/src/main/java/com/novoda/imageloader/core/file/util/FlushedInputStream.java
@@ -20,7 +20,7 @@
import java.io.InputStream;
public class FlushedInputStream extends FilterInputStream {
-
+
public FlushedInputStream(InputStream inputStream) {
super(inputStream);
}
View
92 core/src/main/java/com/novoda/imageloader/core/loader/ConcurrentLoader.java
@@ -25,52 +25,52 @@
import com.novoda.imageloader.core.model.ImageWrapper;
public class ConcurrentLoader implements Loader {
-
- private LoaderContext loaderContext;
-
- public ConcurrentLoader(LoaderContext loaderContext) {
- this.loaderContext = loaderContext;
- }
- @Override
- public void load(ImageView imageView) {
- ImageWrapper w = new ImageWrapper(imageView);
- if(w.getUrl() == null) {
- Log.w("ImageLoader", "You should never call load if you don't set a ImageTag on the view");
- return;
- }
- try {
- if (loaderContext.getCache().hasBitmap(w.getUrl())) {
- Bitmap b = loaderContext.getCache().get(w.getUrl());
- if (b != null) {
- imageView.setImageBitmap(b);
- return;
- }
- }
- setResource(w, w.getLoadingResourceId());
- if(w.isUseCacheOnly()) {
- return;
- }
- new LoaderTask(imageView, loaderContext).execute();
- } catch (ImageNotFoundException inf) {
- setResource(w, w.getNotFoundResourceId());
- } catch (Throwable t) {
- setResource(w, w.getNotFoundResourceId());
- }
- }
-
- private void setResource(ImageWrapper w, int resId) {
- String key = "resource" + resId + w.getHeight() + w.getHeight();
- if(loaderContext.getResBitmapCache().hasBitmap(key)) {
- Bitmap b = loaderContext.getResBitmapCache().get(key);
- if(b != null) {
- w.setBitmap(b);
- return;
- }
- }
- Bitmap b = loaderContext.getBitmapUtil().scaleResourceBitmap(w, resId);
- loaderContext.getResBitmapCache().put(key, b);
- w.setBitmap(b);
- }
+ private LoaderContext loaderContext;
+
+ public ConcurrentLoader(LoaderContext loaderContext) {
+ this.loaderContext = loaderContext;
+ }
+
+ @Override
+ public void load(ImageView imageView) {
+ ImageWrapper w = new ImageWrapper(imageView);
+ if (w.getUrl() == null) {
+ Log.w("ImageLoader", "You should never call load if you don't set a ImageTag on the view");
+ return;
+ }
+ try {
+ if (loaderContext.getCache().hasBitmap(w.getUrl())) {
+ Bitmap b = loaderContext.getCache().get(w.getUrl());
+ if (b != null) {
+ imageView.setImageBitmap(b);
+ return;
+ }
+ }
+ setResource(w, w.getLoadingResourceId());
+ if (w.isUseCacheOnly()) {
+ return;
+ }
+ new LoaderTask(imageView, loaderContext).execute();
+ } catch (ImageNotFoundException inf) {
+ setResource(w, w.getNotFoundResourceId());
+ } catch (Throwable t) {
+ setResource(w, w.getNotFoundResourceId());
+ }
+ }
+
+ private void setResource(ImageWrapper w, int resId) {
+ String key = "resource" + resId + w.getHeight() + w.getHeight();
+ if (loaderContext.getResBitmapCache().hasBitmap(key)) {
+ Bitmap b = loaderContext.getResBitmapCache().get(key);
+ if (b != null) {
+ w.setBitmap(b);
+ return;
+ }
+ }
+ Bitmap b = loaderContext.getBitmapUtil().scaleResourceBitmap(w, resId);
+ loaderContext.getResBitmapCache().put(key, b);
+ w.setBitmap(b);
+ }
}
View
4 core/src/main/java/com/novoda/imageloader/core/loader/Loader.java
@@ -19,6 +19,6 @@
public interface Loader {
- void load(ImageView imageView);
-
+ void load(ImageView imageView);
+
}
View
133 core/src/main/java/com/novoda/imageloader/core/loader/SimpleLoader.java
@@ -28,74 +28,75 @@
public class SimpleLoader implements Loader {
- private LoaderContext loaderContext;
- private SingleThreadedLoader singleThreadedLoader;
+ private LoaderContext loaderContext;
+ private SingleThreadedLoader singleThreadedLoader;
- public SimpleLoader(LoaderContext loaderContext) {
- this.loaderContext = loaderContext;
- this.singleThreadedLoader = new SingleThreadedLoader() {
- @Override
- protected Bitmap loadMissingBitmap(ImageWrapper iw) {
- return getBitmap(iw.getUrl(), iw.getWidth(), iw.getHeight());
- }
- @Override
- protected void onBitmapLoaded(ImageWrapper iw, Bitmap bmp) {
- new BitmapDisplayer(bmp, iw).runOnUiThread();
- SimpleLoader.this.loaderContext.getCache().put(iw.getUrl(), bmp);
- }
- };
- }
+ public SimpleLoader(LoaderContext loaderContext) {
+ this.loaderContext = loaderContext;
+ this.singleThreadedLoader = new SingleThreadedLoader() {
+ @Override
+ protected Bitmap loadMissingBitmap(ImageWrapper iw) {
+ return getBitmap(iw.getUrl(), iw.getWidth(), iw.getHeight());
+ }
- @Override
- public void load(ImageView imageView) {
- ImageWrapper w = new ImageWrapper(imageView);
- try {
- if (loaderContext.getCache().hasBitmap(w.getUrl())) {
- Bitmap b = loaderContext.getCache().get(w.getUrl());
- if (b != null) {
- imageView.setImageBitmap(b);
- return;
- }
- }
- setResource(w, w.getLoadingResourceId());
- if(w.isUseCacheOnly()) {
- return;
- }
- singleThreadedLoader.push(w);
- } catch (ImageNotFoundException inf) {
- setResource(w, w.getNotFoundResourceId());
- } catch (Throwable t) {
- setResource(w, w.getNotFoundResourceId());
- }
- }
+ @Override
+ protected void onBitmapLoaded(ImageWrapper iw, Bitmap bmp) {
+ new BitmapDisplayer(bmp, iw).runOnUiThread();
+ SimpleLoader.this.loaderContext.getCache().put(iw.getUrl(), bmp);
+ }
+ };
+ }
- private Bitmap getBitmap(String url, int width, int height) {
- if (url != null && url.length() >= 0) {
- File f = loaderContext.getFileManager().getFile(url);
- if (f.exists()) {
- Bitmap b = loaderContext.getBitmapUtil().decodeFileAndScale(f, width, height);
- if (b != null) {
- return b;
- }
- }
- loaderContext.getNetworkManager().retrieveImage(url, f);
- return loaderContext.getBitmapUtil().decodeFileAndScale(f, width, height);
- }
- return null;
- }
-
- private void setResource(ImageWrapper w, int resId) {
- String key = "resource" + resId + w.getHeight() + w.getHeight();
- if(loaderContext.getResBitmapCache().hasBitmap(key)) {
- Bitmap b = loaderContext.getResBitmapCache().get(key);
- if(b != null) {
- w.setBitmap(b);
- return;
- }
- }
- Bitmap b = loaderContext.getBitmapUtil().scaleResourceBitmap(w, resId);
- loaderContext.getResBitmapCache().put(key, b);
- w.setBitmap(b);
- }
+ @Override
+ public void load(ImageView imageView) {
+ ImageWrapper w = new ImageWrapper(imageView);
+ try {
+ if (loaderContext.getCache().hasBitmap(w.getUrl())) {
+ Bitmap b = loaderContext.getCache().get(w.getUrl());
+ if (b != null) {
+ imageView.setImageBitmap(b);
+ return;
+ }
+ }
+ setResource(w, w.getLoadingResourceId());
+ if (w.isUseCacheOnly()) {
+ return;
+ }
+ singleThreadedLoader.push(w);
+ } catch (ImageNotFoundException inf) {
+ setResource(w, w.getNotFoundResourceId());
+ } catch (Throwable t) {
+ setResource(w, w.getNotFoundResourceId());
+ }
+ }
+
+ private Bitmap getBitmap(String url, int width, int height) {
+ if (url != null && url.length() >= 0) {
+ File f = loaderContext.getFileManager().getFile(url);
+ if (f.exists()) {
+ Bitmap b = loaderContext.getBitmapUtil().decodeFileAndScale(f, width, height);
+ if (b != null) {
+ return b;
+ }
+ }
+ loaderContext.getNetworkManager().retrieveImage(url, f);
+ return loaderContext.getBitmapUtil().decodeFileAndScale(f, width, height);
+ }
+ return null;
+ }
+
+ private void setResource(ImageWrapper w, int resId) {
+ String key = "resource" + resId + w.getHeight() + w.getHeight();
+ if (loaderContext.getResBitmapCache().hasBitmap(key)) {
+ Bitmap b = loaderContext.getResBitmapCache().get(key);
+ if (b != null) {
+ w.setBitmap(b);
+ return;
+ }
+ }
+ Bitmap b = loaderContext.getBitmapUtil().scaleResourceBitmap(w, resId);
+ loaderContext.getResBitmapCache().put(key, b);
+ w.setBitmap(b);
+ }
}
View
38 core/src/main/java/com/novoda/imageloader/core/loader/util/BitmapDisplayer.java
@@ -20,26 +20,26 @@
import com.novoda.imageloader.core.model.ImageWrapper;
public class BitmapDisplayer implements Runnable {
- private Bitmap bitmap;
- private ImageWrapper imageView;
+ private Bitmap bitmap;
+ private ImageWrapper imageView;
- public BitmapDisplayer(Bitmap b, ImageWrapper i) {
- bitmap = b;
- imageView = i;
- }
-
- public void runOnUiThread() {
- imageView.runOnUiThread(this);
- }
+ public BitmapDisplayer(Bitmap b, ImageWrapper i) {
+ bitmap = b;
+ imageView = i;
+ }
+
+ public void runOnUiThread() {
+ imageView.runOnUiThread(this);
+ }
- @Override
- public void run() {
- if (bitmap == null) {
- return;
+ @Override
+ public void run() {
+ if (bitmap == null) {
+ return;
+ }
+ if (imageView.isUrlChanged()) {
+ return;
+ }
+ imageView.setBitmap(bitmap);
}
- if(imageView.isUrlChanged()) {
- return;
- }
- imageView.setBitmap(bitmap);
- }
}
View
160 core/src/main/java/com/novoda/imageloader/core/loader/util/LoaderTask.java
@@ -28,88 +28,88 @@
public class LoaderTask extends AsyncTask<String, Void, Bitmap> {
- private WeakReference<ImageView> imageViewReference;
- private LoaderContext loaderContext;
- private String url;
+ private WeakReference<ImageView> imageViewReference;
+ private LoaderContext loaderContext;
+ private String url;
- public LoaderTask(ImageView imageView, LoaderContext loaderContext) {
- this.imageViewReference = new WeakReference<ImageView>(imageView);
- this.loaderContext = loaderContext;
- }
+ public LoaderTask(ImageView imageView, LoaderContext loaderContext) {
+ this.imageViewReference = new WeakReference<ImageView>(imageView);
+ this.loaderContext = loaderContext;
+ }
- @Override
- protected Bitmap doInBackground(String... arg0) {
- if (imageViewReference == null) {
- return null;
- }
- ImageView imageView = imageViewReference.get();
- if (imageView == null) {
- return null;
- }
- ImageWrapper w = new ImageWrapper(imageView);
- url = w.getUrl();
- if (url == null || url.length() <= 0) {
- return null;
- }
- int width = w.getWidth();
- int height = w.getHeight();
- if(!url.equals(new ImageWrapper(imageView).getCurrentUrl())) {
- return null;
- }
- File f = loaderContext.getFileManager().getFile(url);
- if (f.exists()) {
- if(!url.equals(new ImageWrapper(imageView).getCurrentUrl())) {
- return null;
- }
- Bitmap b = loaderContext.getBitmapUtil().decodeFileAndScale(f, width, height);
- if (b != null) {
- return b;
- }
- }
- try {
- loaderContext.getNetworkManager().retrieveImage(url, f);
- } catch (ImageNotFoundException inf) {
- return getResource(w, w.getNotFoundResourceId());
- }
- if(!url.equals(new ImageWrapper(imageView).getCurrentUrl())) {
- return null;
- }
- return loaderContext.getBitmapUtil().decodeFileAndScale(f, width, height);
- }
+ @Override
+ protected Bitmap doInBackground(String... arg0) {
+ if (imageViewReference == null) {
+ return null;
+ }
+ ImageView imageView = imageViewReference.get();
+ if (imageView == null) {
+ return null;
+ }
+ ImageWrapper w = new ImageWrapper(imageView);
+ url = w.getUrl();
+ if (url == null || url.length() <= 0) {
+ return null;
+ }
+ int width = w.getWidth();
+ int height = w.getHeight();
+ if (!url.equals(new ImageWrapper(imageView).getCurrentUrl())) {
+ return null;
+ }
+ File f = loaderContext.getFileManager().getFile(url);
+ if (f.exists()) {
+ if (!url.equals(new ImageWrapper(imageView).getCurrentUrl())) {
+ return null;
+ }
+ Bitmap b = loaderContext.getBitmapUtil().decodeFileAndScale(f, width, height);
+ if (b != null) {
+ return b;
+ }
+ }
+ try {
+ loaderContext.getNetworkManager().retrieveImage(url, f);
+ } catch (ImageNotFoundException inf) {
+ return getResource(w, w.getNotFoundResourceId());
+ }
+ if (!url.equals(new ImageWrapper(imageView).getCurrentUrl())) {
+ return null;
+ }
+ return loaderContext.getBitmapUtil().decodeFileAndScale(f, width, height);
+ }
- @Override
- protected void onPostExecute(Bitmap bitmap) {
- if(bitmap == null) {
- return;
- }
- if (isCancelled()) {
- bitmap = null;
- return;
- }
- if (imageViewReference == null) {
- return;
- }
- ImageView imageView = imageViewReference.get();
- if (imageView == null) {
- return;
- }
- if(!url.equals(new ImageWrapper(imageView).getCurrentUrl())) {
- return;
- }
- imageView.setImageBitmap(bitmap);
- }
+ @Override
+ protected void onPostExecute(Bitmap bitmap) {
+ if (bitmap == null) {
+ return;
+ }
+ if (isCancelled()) {
+ bitmap = null;
+ return;
+ }
+ if (imageViewReference == null) {
+ return;
+ }
+ ImageView imageView = imageViewReference.get();
+ if (imageView == null) {
+ return;
+ }
+ if (!url.equals(new ImageWrapper(imageView).getCurrentUrl())) {
+ return;
+ }
+ imageView.setImageBitmap(bitmap);
+ }
+
+ private Bitmap getResource(ImageWrapper w, int resId) {
+ String key = "resource" + resId + w.getHeight() + w.getHeight();
+ if (loaderContext.getResBitmapCache().hasBitmap(key)) {
+ Bitmap b = loaderContext.getResBitmapCache().get(key);
+ if (b != null) {
+ return b;
+ }
+ }
+ Bitmap b = loaderContext.getBitmapUtil().scaleResourceBitmap(w, resId);
+ loaderContext.getResBitmapCache().put(key, b);
+ return b;
+ }
- private Bitmap getResource(ImageWrapper w, int resId) {
- String key = "resource" + resId + w.getHeight() + w.getHeight();
- if(loaderContext.getResBitmapCache().hasBitmap(key)) {
- Bitmap b = loaderContext.getResBitmapCache().get(key);
- if(b != null) {
- return b;
- }
- }
- Bitmap b = loaderContext.getBitmapUtil().scaleResourceBitmap(w, resId);
- loaderContext.getResBitmapCache().put(key, b);
- return b;
- }
-
}
View
205 core/src/main/java/com/novoda/imageloader/core/loader/util/SingleThreadedLoader.java
@@ -28,125 +28,124 @@
public abstract class SingleThreadedLoader {
- private static final String TAG = "ImageLoader";
-
- private BitmapLoader thread = new BitmapLoader();
- private Stack<ImageWrapper> stack = new Stack<ImageWrapper>();
- private List<String> notFoundImages = new ArrayList<String>();
-
- public void push(ImageWrapper image) {
- if (TextUtils.isEmpty(image.getUrl())) {
- return;
- }
- pushOnStack(image);
- startThread();
- }
+ private static final String TAG = "ImageLoader";
- public int size() {
- synchronized (stack) {
- return stack.size();
- }
- }
-
- public ImageWrapper pop() {
- synchronized (stack) {
- try {
- return stack.pop();
- } catch (Exception e) {
- return null;
- }
- }
- }
-
- protected abstract Bitmap loadMissingBitmap(ImageWrapper iw);
-
- protected abstract void onBitmapLoaded(ImageWrapper iw, Bitmap bmp);
-
- private void clean(ImageWrapper p) {
- synchronized (stack) {
- for (int j = 0; j < stack.size(); j++) {
- if (stack.get(j).getUrl() != null
- && stack.get(j).getUrl().equals(p.getUrl())) {
- stack.remove(j);
- j--;
- }
- }
- }
- }
+ private BitmapLoader thread = new BitmapLoader();
+ private Stack<ImageWrapper> stack = new Stack<ImageWrapper>();
+ private List<String> notFoundImages = new ArrayList<String>();
- private void pushOnStack(ImageWrapper p) {
- synchronized (stack) {
- stack.push(p);
+ public void push(ImageWrapper image) {
+ if (TextUtils.isEmpty(image.getUrl())) {
+ return;
+ }
+ pushOnStack(image);
+ startThread();
}
- }
- private class BitmapLoader extends Thread {
- boolean isWaiting = false;
-
- public BitmapLoader() {
- setPriority(Thread.NORM_PRIORITY - 1);
+ public int size() {
+ synchronized (stack) {
+ return stack.size();
+ }
}
- @Override
- public void run() {
- while (true) {
- pauseThreadIfnecessary();
- ImageWrapper image = pop();
- if(image != null) {
- loadAndShowImage(image);
+ public ImageWrapper pop() {
+ synchronized (stack) {
+ try {
+ return stack.pop();
+ } catch (Exception e) {
+ return null;
+ }
}
- }
}
- private void pauseThreadIfnecessary() {
- if (size() != 0) {
- return;
- }
- synchronized (thread) {
- try {
- isWaiting = true;
- wait();
- } catch (Exception e) {
- Log.v(TAG, "Pausing the thread error " + e.getMessage());
+ protected abstract Bitmap loadMissingBitmap(ImageWrapper iw);
+
+ protected abstract void onBitmapLoaded(ImageWrapper iw, Bitmap bmp);
+
+ private void clean(ImageWrapper p) {
+ synchronized (stack) {
+ for (int j = 0; j < stack.size(); j++) {
+ if (stack.get(j).getUrl() != null && stack.get(j).getUrl().equals(p.getUrl())) {
+ stack.remove(j);
+ j--;
+ }
+ }
}
- }
}
- private void loadAndShowImage(ImageWrapper iw) {
- try {
- if(iw.isUrlChanged()) {
- return;
- }
- Bitmap bmp = loadMissingBitmap(iw);
- if (bmp == null) {
- clean(iw);
- onBitmapLoaded(iw, bmp);
- return;
+ private void pushOnStack(ImageWrapper p) {
+ synchronized (stack) {
+ stack.push(p);
}
- onBitmapLoaded(iw, bmp);
- } catch(ImageNotFoundException inf) {
- notFoundImages.add(iw.getUrl());
- } catch (Throwable e) {
- Log.e(TAG, "Throwable : " + e.getMessage(), e);
- }
}
- }
-
- private void startThread() {
- if (thread.getState() == Thread.State.NEW) {
- thread.start();
- return;
+
+ private class BitmapLoader extends Thread {
+ boolean isWaiting = false;
+
+ public BitmapLoader() {
+ setPriority(Thread.NORM_PRIORITY - 1);
+ }
+
+ @Override
+ public void run() {
+ while (true) {
+ pauseThreadIfnecessary();
+ ImageWrapper image = pop();
+ if (image != null) {
+ loadAndShowImage(image);
+ }
+ }
+ }
+
+ private void pauseThreadIfnecessary() {
+ if (size() != 0) {
+ return;
+ }
+ synchronized (thread) {
+ try {
+ isWaiting = true;
+ wait();
+ } catch (Exception e) {
+ Log.v(TAG, "Pausing the thread error " + e.getMessage());
+ }
+ }
+ }
+
+ private void loadAndShowImage(ImageWrapper iw) {
+ try {
+ if (iw.isUrlChanged()) {
+ return;
+ }
+ Bitmap bmp = loadMissingBitmap(iw);
+ if (bmp == null) {
+ clean(iw);
+ onBitmapLoaded(iw, bmp);
+ return;
+ }
+ onBitmapLoaded(iw, bmp);
+ } catch (ImageNotFoundException inf) {
+ notFoundImages.add(iw.getUrl());
+ } catch (Throwable e) {
+ Log.e(TAG, "Throwable : " + e.getMessage(), e);
+ }
+ }
}
- synchronized (thread) {
- if (thread.isWaiting) {
- try {
- thread.isWaiting = false;
- thread.notify();
- } catch (Exception ie) {
- Log.e(TAG, "Check and resume the thread " + ie.getMessage());
+
+ private void startThread() {
+ if (thread.getState() == Thread.State.NEW) {
+ thread.start();
+ return;
+ }
+ synchronized (thread) {
+ if (thread.isWaiting) {
+ try {
+ thread.isWaiting = false;
+ thread.notify();
+ } catch (Exception ie) {