diff options
author | Ray Chen <raychen@google.com> | 2009-05-13 16:21:52 +0800 |
---|---|---|
committer | Chih-Chung Chang <chihchung@google.com> | 2009-05-14 18:37:07 +0800 |
commit | 896182a590fc1bbed5dfb78a74e9a71b30d8f9aa (patch) | |
tree | 86d4577a6b30aa16200d8558930d57444c69e482 | |
parent | e43adb123b34432b977f57943c3c42614ccfd985 (diff) | |
download | packages_apps_LegacyCamera-896182a590fc1bbed5dfb78a74e9a71b30d8f9aa.zip packages_apps_LegacyCamera-896182a590fc1bbed5dfb78a74e9a71b30d8f9aa.tar.gz packages_apps_LegacyCamera-896182a590fc1bbed5dfb78a74e9a71b30d8f9aa.tar.bz2 |
remove cancelAllDecodingThreads and allowAllDecodingThreads.
-rw-r--r-- | src/com/android/camera/BitmapManager.java | 83 | ||||
-rw-r--r-- | src/com/android/camera/CropImage.java | 19 | ||||
-rw-r--r-- | src/com/android/camera/ImageGallery.java | 6 | ||||
-rw-r--r-- | src/com/android/camera/ViewImage.java | 10 | ||||
-rw-r--r-- | tests/src/com/android/camera/unit/BitmapManagerUnitTest.java | 18 |
5 files changed, 61 insertions, 75 deletions
diff --git a/src/com/android/camera/BitmapManager.java b/src/com/android/camera/BitmapManager.java index 353da52..c70db78 100644 --- a/src/com/android/camera/BitmapManager.java +++ b/src/com/android/camera/BitmapManager.java @@ -56,19 +56,28 @@ public class BitmapManager { return s; } } + + static class ThreadGroup implements Iterable<Thread> { + private final WeakHashMap<Thread, Object> mWeakCollection = + new WeakHashMap<Thread, Object>(); + + public void add(Thread t) { + mWeakCollection.put(t, null); + } + public void remove(Thread t) { + mWeakCollection.remove(t); + } + public Iterator<Thread> iterator() { + return mWeakCollection.keySet().iterator(); + } + } + private final WeakHashMap<Thread, ThreadStatus> mThreadStatus = new WeakHashMap<Thread, ThreadStatus>(); - private boolean mAllowDecoding = false; + private boolean mLocked = false; private boolean mCheckResourceLock = false; - private static BitmapManager sManager; - - public static BitmapManager instance() { - if (sManager == null) { - sManager = new BitmapManager(); - } - return sManager; - } + private static BitmapManager sManager = null; private BitmapManager() { } @@ -146,6 +155,25 @@ public class BitmapManager { status.options = null; } + public synchronized void allowThreadDecoding(ThreadGroup threads) { + for (Thread t : threads) { + getThreadStatus(t, true).state = State.WAIT; + } + } + + public synchronized void cancelThreadDecoding(ThreadGroup threads) { + for (Thread t : threads) { + ThreadStatus status = getThreadStatus(t, true); + status.state = State.CANCEL; + if (status.options != null) { + status.options.requestCancelDecode(); + } + } + + // Wake up threads in waiting list + notifyAll(); + } + /** * The following three methods are used to keep track of which thread * is being disabled for bitmap decoding. @@ -182,7 +210,6 @@ public class BitmapManager { * bitmap decoding. */ public synchronized void cancelAllDecoding() { - mAllowDecoding = false; for (ThreadStatus status : mThreadStatus.values()) { status.state = State.CANCEL; if (status.options != null) { @@ -194,19 +221,8 @@ public class BitmapManager { notifyAll(); } - public synchronized void allowAllDecoding() { - allowAllDecoding(true); - } - - public synchronized void allowAllDecoding(boolean reset) { - mAllowDecoding = true; - if (reset) { - mThreadStatus.clear(); - } - } - - public synchronized boolean canDecode() { - return mAllowDecoding; + public synchronized void resetThreadStatus() { + mThreadStatus.clear(); } /** @@ -224,6 +240,13 @@ public class BitmapManager { } } + public static synchronized BitmapManager instance() { + if (sManager == null) { + sManager = new BitmapManager(); + } + return sManager; + } + /** * The real place to delegate bitmap decoding to BitmapFactory. */ @@ -233,31 +256,21 @@ public class BitmapManager { return null; } - // Does the global switch turn on? - if (!canDecode()) { - // This is a bug, and we should fix the caller. - Util.debugWhere(TAG, "canDecode() == false"); - return null; - } - - // Can current thread decode? Thread thread = Thread.currentThread(); if (!canThreadDecoding(thread)) { - // This is a bug, and we should fix the caller. - Util.debugWhere(TAG, "canThreadDecoding() == false"); + Log.d(TAG, "Thread " + thread + " is not allowed to decode."); return null; } setDecodingOptions(thread, options); - Bitmap b = BitmapFactory.decodeFileDescriptor(fd, null, options); // In case legacy code cancel it in traditional way if (options.mCancel) { cancelThreadDecoding(thread); } - removeDecodingOptions(thread); + removeDecodingOptions(thread); return b; } } diff --git a/src/com/android/camera/CropImage.java b/src/com/android/camera/CropImage.java index a64b02c..db796fb 100644 --- a/src/com/android/camera/CropImage.java +++ b/src/com/android/camera/CropImage.java @@ -77,6 +77,7 @@ public class CropImage extends Activity { private Bitmap mBitmap; private Bitmap mCroppedImage; + private BitmapManager.ThreadGroup mDecodingThreads = new BitmapManager.ThreadGroup(); HighlightView mCrop; private IImage mImage; @@ -102,10 +103,6 @@ public class CropImage extends Activity { MenuHelper.showStorageToast(this); - BitmapManager bitmapManager = BitmapManager.instance(); - bitmapManager.setCheckResourceLock(false); - bitmapManager.allowAllDecoding(); - try { Intent intent = getIntent(); Bundle extras = intent.getExtras(); @@ -192,7 +189,7 @@ public class CropImage extends Activity { mImageView.center(true, true); } - new Thread(new Runnable() { + Thread t = new Thread(new Runnable() { public void run() { final Bitmap b = (mImage != null) ? mImage.fullSizeBitmap(500) @@ -211,7 +208,9 @@ public class CropImage extends Activity { } }); } - }).start(); + }); + mDecodingThreads.add(t); + t.start(); } private void onSaveClicked() { @@ -402,15 +401,9 @@ public class CropImage extends Activity { } @Override - public void onResume() { - super.onResume(); - BitmapManager.instance().allowAllDecoding(); - } - - @Override public void onPause() { super.onPause(); - BitmapManager.instance().cancelAllDecoding(); + BitmapManager.instance().cancelThreadDecoding(mDecodingThreads); } Handler mHandler = new Handler(); diff --git a/src/com/android/camera/ImageGallery.java b/src/com/android/camera/ImageGallery.java index a8db347..9447981 100644 --- a/src/com/android/camera/ImageGallery.java +++ b/src/com/android/camera/ImageGallery.java @@ -337,8 +337,8 @@ public class ImageGallery extends Activity implements super.onPause(); mPausing = true; - BitmapManager.instance().cancelAllDecoding(); - stopCheckingThumbnails(); + mLoader.stop(); + mGvs.stop(); if (mReceiver != null) { @@ -399,8 +399,6 @@ public class ImageGallery extends Activity implements public void onResume() { super.onResume(); - BitmapManager.instance().allowAllDecoding(); - mGvs.setSizeChoice(Integer.parseInt( mPrefs.getString("pref_gallery_size_key", "1"))); mGvs.requestFocus(); diff --git a/src/com/android/camera/ViewImage.java b/src/com/android/camera/ViewImage.java index 3ae4cb3..2b171e5 100644 --- a/src/com/android/camera/ViewImage.java +++ b/src/com/android/camera/ViewImage.java @@ -504,11 +504,6 @@ public class ViewImage extends Activity implements View.OnClickListener { mCache = new BitmapCache(3); mImageView.setRecycler(mCache); - - BitmapManager bitmapManager = BitmapManager.instance(); - bitmapManager.setCheckResourceLock(false); - bitmapManager.allowAllDecoding(); - makeGetter(); mAnimationIndex = -1; @@ -891,8 +886,6 @@ public class ViewImage extends Activity implements View.OnClickListener { public void onStart() { super.onStart(); - BitmapManager.instance().allowAllDecoding(false); - init(mSavedUri); // normally this will never be zero but if one "backs" into this @@ -919,7 +912,6 @@ public class ViewImage extends Activity implements View.OnClickListener { @Override public void onStop() { super.onStop(); - BitmapManager.instance().cancelAllDecoding(); mGetter.cancelCurrent(); mGetter.stop(); @@ -1382,7 +1374,6 @@ class ImageGetter { mViewImage = viewImage; mGetterThread = new Thread(new ImageGetterRunnable()); mGetterThread.setName("ImageGettter"); - BitmapManager.instance().allowThreadDecoding(mGetterThread); mGetterThread.start(); } @@ -1426,6 +1417,7 @@ class ImageGetter { ImageGetter.this.notify(); } try { + BitmapManager.instance().cancelThreadDecoding(mGetterThread); mGetterThread.join(); } catch (InterruptedException ex) { // Ignore the exception diff --git a/tests/src/com/android/camera/unit/BitmapManagerUnitTest.java b/tests/src/com/android/camera/unit/BitmapManagerUnitTest.java index 4968718..2a11ae0 100644 --- a/tests/src/com/android/camera/unit/BitmapManagerUnitTest.java +++ b/tests/src/com/android/camera/unit/BitmapManagerUnitTest.java @@ -6,6 +6,7 @@ import com.android.camera.ImageManager; import com.android.camera.gallery.IImage; import com.android.camera.gallery.IImageList; +import android.content.Context; import android.graphics.Bitmap; import android.test.AndroidTestCase; @@ -16,6 +17,7 @@ public class BitmapManagerUnitTest extends AndroidTestCase { IImageList mImageList; IImage mImage; BitmapManager mBitmapManager; + Context mContext; private class DecodeThread extends Thread { Bitmap bitmap; @@ -41,8 +43,9 @@ public class BitmapManagerUnitTest extends AndroidTestCase { } public void setUp() { + mContext = getContext(); mBitmapManager = BitmapManager.instance(); - mImageList = ImageManager.allImages(getContext().getContentResolver(), + mImageList = ImageManager.allImages(mContext.getContentResolver(), ImageManager.DataLocation.ALL, ImageManager.INCLUDE_IMAGES, ImageManager.SORT_DESCENDING); @@ -56,16 +59,7 @@ public class BitmapManagerUnitTest extends AndroidTestCase { assertSame(manager, mBitmapManager); } - public void testCanDecoding() { - assertFalse(mBitmapManager.canDecode()); - mBitmapManager.allowAllDecoding(); - assertTrue(mBitmapManager.canDecode()); - mBitmapManager.cancelAllDecoding(); - assertFalse(mBitmapManager.canDecode()); - } - public void testCheckResourceLockWithoutAcquiringLock() { - mBitmapManager.allowAllDecoding(); DecodeThread t = new DecodeThread(false); assertTrue(mBitmapManager.canThreadDecoding(t)); mBitmapManager.setCheckResourceLock(true); @@ -81,7 +75,6 @@ public class BitmapManagerUnitTest extends AndroidTestCase { } public void testCheckResourceLockWithAcquiringLock() { - mBitmapManager.allowAllDecoding(); DecodeThread t1 = new DecodeThread(true); DecodeThread t2 = new DecodeThread(true); assertTrue(mBitmapManager.canThreadDecoding(t1)); @@ -119,7 +112,6 @@ public class BitmapManagerUnitTest extends AndroidTestCase { public void testDecoding() { assertNotNull(mImage); - mBitmapManager.allowAllDecoding(); mBitmapManager.setCheckResourceLock(false); Bitmap bitmap = mImage.thumbBitmap(); assertNotNull(bitmap); @@ -131,7 +123,6 @@ public class BitmapManagerUnitTest extends AndroidTestCase { } public void testCanThreadDecoding() { - mBitmapManager.allowAllDecoding(); Thread t = new DecodeThread(false); // By default all threads can decode. @@ -149,7 +140,6 @@ public class BitmapManagerUnitTest extends AndroidTestCase { public void testThreadDecoding() { DecodeThread t1 = new DecodeThread(false); DecodeThread t2 = new DecodeThread(false); - mBitmapManager.allowAllDecoding(); mBitmapManager.setCheckResourceLock(false); mBitmapManager.allowThreadDecoding(t1); mBitmapManager.cancelThreadDecoding(t2); |