Merge "Added locking to ImageLoader." into gb-ub-photos-arches
diff --git a/src/com/android/gallery3d/filtershow/cache/ImageLoader.java b/src/com/android/gallery3d/filtershow/cache/ImageLoader.java
index a891996..21548b1 100644
--- a/src/com/android/gallery3d/filtershow/cache/ImageLoader.java
+++ b/src/com/android/gallery3d/filtershow/cache/ImageLoader.java
@@ -50,6 +50,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
+import java.util.concurrent.locks.ReentrantLock;
public class ImageLoader {
@@ -83,6 +84,8 @@
private Rect mOriginalBounds = null;
private static int mZoomOrientation = ORI_NORMAL;
+ private ReentrantLock mLoadingLock = new ReentrantLock();
+
public ImageLoader(FilterShowActivity activity, Context context) {
mActivity = activity;
mContext = context;
@@ -99,6 +102,7 @@
}
public void loadBitmap(Uri uri,int size) {
+ mLoadingLock.lock();
mUri = uri;
mOrientation = getOrientation(mContext, uri);
mOriginalBitmapSmall = loadScaledBitmap(uri, 160);
@@ -108,6 +112,7 @@
}
mOriginalBitmapLarge = loadScaledBitmap(uri, size);
updateBitmaps();
+ mLoadingLock.unlock();
}
public Uri getUri() {
@@ -308,10 +313,12 @@
}
public void addListener(ImageShow imageShow) {
+ mLoadingLock.lock();
if (!mListeners.contains(imageShow)) {
mListeners.add(imageShow);
}
mHiresCache.addObserver(imageShow);
+ mLoadingLock.unlock();
}
private void warnListeners() {
@@ -333,6 +340,7 @@
// move this to a background thread.
public Bitmap getScaleOneImageForPreset(ImageShow caller, ImagePreset imagePreset, Rect bounds,
boolean force) {
+ mLoadingLock.lock();
Bitmap bmp = mZoomCache.getImage(imagePreset, bounds);
if (force || bmp == null) {
bmp = loadRegionBitmap(mUri, bounds);
@@ -347,12 +355,14 @@
return bmp2;
}
}
+ mLoadingLock.unlock();
return bmp;
}
// Caching method
public Bitmap getImageForPreset(ImageShow caller, ImagePreset imagePreset,
boolean hiRes) {
+ mLoadingLock.lock();
if (mOriginalBitmapSmall == null) {
return null;
}
@@ -377,13 +387,16 @@
mCache.addObserver(caller);
}
}
+ mLoadingLock.unlock();
return filteredImage;
}
public void resetImageForPreset(ImagePreset imagePreset, ImageShow caller) {
+ mLoadingLock.lock();
mHiresCache.reset(imagePreset);
mCache.reset(imagePreset);
mZoomCache.reset(imagePreset);
+ mLoadingLock.unlock();
}
public void saveImage(ImagePreset preset, final FilterShowActivity filterShowActivity,