Permalink
Browse files

removing LoaderContext as it was needlessly wrapping LoaderSettings -

loadersettings is now the only way to configure the imageloader
added setting custom loader, filemanager, networkmanager, resCachemanager and cachemanager (also added to the settingsbuilder). addresses issue #48
  • Loading branch information...
1 parent f304ede commit 68b88d0a164039c85bac2c9bc578582b42a3008e @ouchadam ouchadam committed Jan 15, 2013
@@ -22,10 +22,7 @@ public void testCacheImage() {
LoaderSettings settings = new LoaderSettings();
- LoaderContext loaderContext = new LoaderContext();
- loaderContext.setSettings(settings);
-
- ImageManager imageManager = new ImageManager(getInstrumentation().getTargetContext(), loaderContext);
+ ImageManager imageManager = new ImageManager(getInstrumentation().getTargetContext(), settings);
imageManager.cacheImage("http://king.com/img.png", 100, 100);
}
@@ -19,18 +19,17 @@
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
+
import com.novoda.imageloader.core.cache.CacheManager;
-import com.novoda.imageloader.core.cache.SoftMapCache;
import com.novoda.imageloader.core.exception.ImageNotFoundException;
-import com.novoda.imageloader.core.file.BasicFileManager;
import com.novoda.imageloader.core.file.FileManager;
-import com.novoda.imageloader.core.loader.ConcurrentLoader;
import com.novoda.imageloader.core.loader.Loader;
-import com.novoda.imageloader.core.loader.SimpleLoader;
import com.novoda.imageloader.core.network.NetworkManager;
-import com.novoda.imageloader.core.network.UrlNetworkManager;
import java.io.File;
+import java.lang.ref.WeakReference;
+import java.util.HashMap;
+import java.util.Map;
/**
* ImageManager has the responsibility to provide a
@@ -44,99 +43,56 @@
*/
public class ImageManager {
- private LoaderContext loaderContext;
- private Loader loader;
- private CacheManager cacheManager;
+ private final LoaderSettings loaderSettings;
+ private final Map<Integer, WeakReference<OnImageLoadedListener>> onImageLoadedListeners;
- public ImageManager(Context context, LoaderSettings settings) {
- this.loaderContext = new LoaderContext();
- loaderContext.setSettings(settings);
- loaderContext.setFileManager(new BasicFileManager(settings));
- loaderContext.setNetworkManager(new UrlNetworkManager(settings));
- loaderContext.setResBitmapCache(new SoftMapCache());
- cacheManager = settings.getCacheManager();
- if (cacheManager == null) {
- cacheManager = new SoftMapCache();
- }
- loaderContext.setCache(cacheManager);
- setLoader(settings);
- verifyPermissions(context);
+ public ImageManager(LoaderSettings settings) {
+ this(null, settings);
}
- /**
- * Constructor for advanced use. The loaderContext has to be setup correctly.
- *
- * @param context context where this image manager is used
- * @param loaderContext pre-configured loader context
- */
- public ImageManager(Context context, LoaderContext loaderContext) {
- assert (loaderContext.getSettings() != null);
- LoaderSettings settings = loaderContext.getSettings();
- if (loaderContext.getFileManager() == null) {
- loaderContext.setFileManager(new BasicFileManager(settings));
- }
- if (loaderContext.getNetworkManager() == null) {
- loaderContext.setNetworkManager(new UrlNetworkManager(settings));
- }
- cacheManager = settings.getCacheManager();
- if (cacheManager == null) {
- cacheManager = new SoftMapCache();
+ public ImageManager(Context context, LoaderSettings settings) {
+ if (context != null) {
+ verifyPermissions(context);
}
- loaderContext.setCache(cacheManager);
-
- this.loaderContext = loaderContext;
+ this.loaderSettings = settings;
+ onImageLoadedListeners = new HashMap<Integer, WeakReference<OnImageLoadedListener>>();
+ }
- setLoader(settings);
- verifyPermissions(context);
+ 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");
+ }
}
public Loader getLoader() {
- return loader;
+ return loaderSettings.getLoader();
}
public FileManager getFileManager() {
- return loaderContext.getFileManager();
+ return loaderSettings.getFileManager();
}
public NetworkManager getNetworkManager() {
- return loaderContext.getNetworkManager();
+ return loaderSettings.getNetworkManager();
}
public CacheManager getCacheManager() {
- return cacheManager;
- }
-
- public void setCacheManager(CacheManager cacheManager) {
- this.cacheManager = cacheManager;
- }
-
- protected void setLoader(LoaderSettings settings) {
- if (settings.isUseAsyncTasks()) {
- this.loader = new ConcurrentLoader(loaderContext);
- } else {
- this.loader = new SimpleLoader(loaderContext);
- }
- }
-
- 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");
- }
+ return loaderSettings.getCacheManager();
}
public void setOnImageLoadedListener(OnImageLoadedListener listener) {
- loaderContext.setListener(listener);
+ onImageLoadedListeners.put(listener.hashCode(), new WeakReference<OnImageLoadedListener>(listener));
+ loaderSettings.getLoader().setLoadListener(onImageLoadedListeners.get(listener.hashCode()));
}
public void unRegisterOnImageLoadedListener(OnImageLoadedListener listener) {
- loaderContext.removeOnImageLoadedListener(listener.hashCode());
+ onImageLoadedListeners.remove(listener.hashCode());
}
/**
@@ -155,24 +111,24 @@ public void unRegisterOnImageLoadedListener(OnImageLoadedListener listener) {
* @height size of the cached image
*/
public void cacheImage(String url, int width, int height) {
- Bitmap bm = loaderContext.getCache().get(url, width, height);
+ Bitmap bm = loaderSettings.getCacheManager().get(url, width, height);
if (bm == null) {
try {
- File imageFile = loaderContext.getFileManager().getFile(url, width, height);
+ File imageFile = loaderSettings.getFileManager().getFile(url, width, height);
if (!imageFile.exists()) {
- loaderContext.getNetworkManager().retrieveImage(url, imageFile);
+ loaderSettings.getNetworkManager().retrieveImage(url, imageFile);
}
Bitmap b;
- if (loaderContext.getSettings().isAlwaysUseOriginalSize()) {
- b = loaderContext.getBitmapUtil().decodeFile(imageFile, width, height);
+ if (loaderSettings.isAlwaysUseOriginalSize()) {
+ b = loaderSettings.getBitmapUtil().decodeFile(imageFile, width, height);
} else {
- b = loaderContext.getBitmapUtil().decodeFileAndScale(imageFile, width, height, loaderContext.getSettings().isAllowUpsampling());
+ b = loaderSettings.getBitmapUtil().decodeFileAndScale(imageFile, width, height, loaderSettings.isAllowUpsampling());
}
if (b == null) {
// decode failed
- loaderContext.getCache().put(url, b);
+ loaderSettings.getCacheManager().put(url, b);
}
@@ -1,105 +0,0 @@
-/**
- * Copyright 2012 Novoda Ltd
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.novoda.imageloader.core;
-
-import com.novoda.imageloader.core.bitmap.BitmapUtil;
-import com.novoda.imageloader.core.cache.CacheManager;
-import com.novoda.imageloader.core.file.FileManager;
-import com.novoda.imageloader.core.network.NetworkManager;
-
-import java.lang.ref.WeakReference;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * LoaderContext provides a generic context for the imageLoader where different objects can access different levels of caching, the BitmapUtil, and all the
- * customized settings.
- *
- * This class is supposed to be used internally.
- */
-public class LoaderContext {
- private FileManager fileManager;
- private NetworkManager networkManager;
- private CacheManager cache;
- private CacheManager resBitmapCache;
- private LoaderSettings settings;
- private final BitmapUtil bitmapUtil = new BitmapUtil();
- private final Map<Integer, WeakReference<OnImageLoadedListener>> weakListeners;
- private int listenerKey;
-
- public LoaderContext() {
- weakListeners = new HashMap<Integer, WeakReference<OnImageLoadedListener>>();
- }
-
- 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;
- }
-
- public void setListener(OnImageLoadedListener listener) {
- listenerKey = listener.hashCode();
- WeakReference<OnImageLoadedListener> weakReference = new WeakReference<OnImageLoadedListener>(listener);
- weakListeners.put(listenerKey, weakReference);
- }
-
- public WeakReference<OnImageLoadedListener> getListener() {
- return weakListeners.get(listenerKey);
- }
-
- public void removeOnImageLoadedListener(int listenerKey) {
- weakListeners.remove(listenerKey);
- }
-
-}
Oops, something went wrong.

0 comments on commit 68b88d0

Please sign in to comment.