Back to project page Joetz-Android-V2.
The source code is released under:
GNU General Public License
If you think the Android project Joetz-Android-V2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.jens.myapplication.apimanager.manager; //from ww w . j a v a 2 s. c o m import android.graphics.drawable.Drawable; import android.util.Log; import com.example.jens.myapplication.apimanager.ApiConnection; import com.example.jens.myapplication.apimanager.CancellableTask; import org.joda.time.DateTime; import java.lang.ref.WeakReference; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; /** * Class for loading images <br /> * No longer in use due to ImageLoader library being used now */ @Deprecated public class ImageManager { //private static final int MAX_TASKS = 3; private static final String URL = "api/images/"; private static final int TIME_DEAD_REF_CHECK = 30; private DateTime nextCheck = DateTime.now().plusSeconds(TIME_DEAD_REF_CHECK); private Map<Integer, WeakReference<Drawable>> images; private List<Integer> currentlyLoadingList; private Map<Integer, List<OnImageLoadedListener>> mapWaitingForImage; private Object lock = new Object(); public ImageManager(){ images = new HashMap<Integer, WeakReference<Drawable>>(); currentlyLoadingList = new LinkedList<Integer>(); mapWaitingForImage = new HashMap<Integer, List<OnImageLoadedListener>>(); } public boolean isLoadingImage(int id){ for(int i : currentlyLoadingList){ if(i == id){ return true; } } return false; } public CancellableTask retrieveImage(final int id, final OnImageLoadedListener callback){ synchronized(lock){ Drawable checkImg = getImage(id); if(checkImg != null){ callback.onImageLoaded(checkImg); return null; } if(currentlyLoadingList.contains(id)){ List<OnImageLoadedListener> listWaiting = mapWaitingForImage.get(id); if(listWaiting == null){ listWaiting = new LinkedList<OnImageLoadedListener>(); mapWaitingForImage.put(id, listWaiting); } listWaiting.add(callback); return null; } Log.d("ImageManager", "Retrieving new image: " + id); currentlyLoadingList.add(id); OnImageLoadedListener listener = new OnImageLoadedListener() { @Override public void onImageLoaded(Drawable drawable) { synchronized(lock){ images.put(id, new WeakReference<Drawable>(drawable)); callback.onImageLoaded(drawable); List<OnImageLoadedListener> listWaiting = mapWaitingForImage.get(id); if(listWaiting != null){ for(OnImageLoadedListener l : listWaiting){ l.onImageLoaded(drawable); } mapWaitingForImage.remove(id); } currentlyLoadingList.remove((Integer) id); } } }; String action = this.URL + id; return ApiConnection.getImage(action, listener); } } public Drawable getImage(int id){ if(DateTime.now().isAfter(nextCheck)){ checkDeadReferences(); } WeakReference<Drawable> wr = images.get(id); if(wr == null){ return null; } Drawable d = wr.get(); if(d == null){ images.remove(id); } return d; } public boolean existsImage(int id){ Drawable r = getImage(id); return r != null; } public void checkDeadReferences(){ Iterator<Integer> it = images.keySet().iterator(); int count = 0; while(it.hasNext()){ if(images.get(it.next()).get() == null){ count++; it.remove(); } } Log.d("ImageManager", "Removed " + count + " dead weak references."); nextCheck = DateTime.now().plusSeconds(TIME_DEAD_REF_CHECK); } public interface OnImageLoadedListener{ public void onImageLoaded(Drawable drawable); } }