From 15b351a22d02e89d882fc9fe32b3f4c512080e0a Mon Sep 17 00:00:00 2001 From: Chih-Chung Chang Date: Thu, 15 Mar 2012 16:38:45 +0800 Subject: Create a ScreenNail interface so we can add other types of screenails. Add a new MediaItem type to contain a ScreenNail. Change-Id: Ia303949f3013dd48ded204eaf9ec69a102b8503e --- .../android/gallery3d/app/PhotoDataAdapter.java | 57 ++++++++++++---------- 1 file changed, 30 insertions(+), 27 deletions(-) (limited to 'src/com/android/gallery3d/app/PhotoDataAdapter.java') diff --git a/src/com/android/gallery3d/app/PhotoDataAdapter.java b/src/com/android/gallery3d/app/PhotoDataAdapter.java index 0b544b95a..82225d919 100644 --- a/src/com/android/gallery3d/app/PhotoDataAdapter.java +++ b/src/com/android/gallery3d/app/PhotoDataAdapter.java @@ -29,8 +29,9 @@ import com.android.gallery3d.data.MediaItem; import com.android.gallery3d.data.MediaObject; import com.android.gallery3d.data.MediaSet; import com.android.gallery3d.data.Path; +import com.android.gallery3d.ui.BitmapScreenNail; import com.android.gallery3d.ui.PhotoView; -import com.android.gallery3d.ui.PhotoView.ImageData; +import com.android.gallery3d.ui.ScreenNail; import com.android.gallery3d.ui.SynchronizedHandler; import com.android.gallery3d.ui.TileImageViewAdapter; import com.android.gallery3d.util.Future; @@ -215,20 +216,22 @@ public class PhotoDataAdapter implements PhotoPage.Model { mDataListener = listener; } - private void updateScreenNail(long version, Future future) { + private void updateScreenNail(long version, Future future) { ImageEntry entry = mImageCache.get(version); + ScreenNail screenNail = future.get(); + if (entry == null || entry.screenNailTask != future) { - Bitmap screenNail = future.get(); if (screenNail != null) screenNail.recycle(); return; } entry.screenNailTask = null; - entry.screenNail = future.get(); + entry.screenNail = screenNail; - if (entry.screenNail == null) { + if (screenNail == null) { entry.failToLoad = true; } + if (mDataListener != null) { mDataListener.onPhotoAvailable(version, false); } @@ -291,24 +294,19 @@ public class PhotoDataAdapter implements PhotoPage.Model { mTileProvider.clear(); } - private ImageData getImage(int index) { + private ScreenNail getImage(int index) { if (index < 0 || index >= mSize || !mIsActive) return null; Utils.assertTrue(index >= mActiveStart && index < mActiveEnd); ImageEntry entry = mImageCache.get(getVersion(index)); - Bitmap screennail = entry == null ? null : entry.screenNail; - if (screennail != null) { - return new ImageData(screennail, entry.rotation); - } else { - return new ImageData(null, 0); - } + return entry == null ? null : entry.screenNail; } - public ImageData getPreviousImage() { + public ScreenNail getPrevScreenNail() { return getImage(mCurrentIndex - 1); } - public ImageData getNextImage() { + public ScreenNail getNextScreenNail() { return getImage(mCurrentIndex + 1); } @@ -343,8 +341,8 @@ public class PhotoDataAdapter implements PhotoPage.Model { updateCurrentIndex(index); } - public Bitmap getBackupImage() { - return mTileProvider.getBackupImage(); + public ScreenNail getScreenNail() { + return mTileProvider.getScreenNail(); } public int getImageHeight() { @@ -409,17 +407,17 @@ public class PhotoDataAdapter implements PhotoPage.Model { } private void updateTileProvider(ImageEntry entry) { - Bitmap screenNail = entry.screenNail; + ScreenNail screenNail = entry.screenNail; BitmapRegionDecoder fullImage = entry.fullImage; if (screenNail != null) { if (fullImage != null) { - mTileProvider.setBackupImage(screenNail, + mTileProvider.setScreenNail(screenNail, fullImage.getWidth(), fullImage.getHeight()); mTileProvider.setRegionDecoder(fullImage); } else { int width = screenNail.getWidth(); int height = screenNail.getHeight(); - mTileProvider.setBackupImage(screenNail, width, height); + mTileProvider.setScreenNail(screenNail, width, height); } } else { mTileProvider.clear(); @@ -489,7 +487,7 @@ public class PhotoDataAdapter implements PhotoPage.Model { } } - private static class ScreenNailJob implements Job { + private static class ScreenNailJob implements Job { private MediaItem mItem; public ScreenNailJob(MediaItem item) { @@ -497,14 +495,19 @@ public class PhotoDataAdapter implements PhotoPage.Model { } @Override - public Bitmap run(JobContext jc) { + public ScreenNail run(JobContext jc) { + // We try to get a ScreenNail first, if it fails, we fallback to get + // a Bitmap and then wrap it in a BitmapScreenNail instead. + ScreenNail s = mItem.getScreenNail(); + if (s != null) return s; + Bitmap bitmap = mItem.requestImage(MediaItem.TYPE_THUMBNAIL).run(jc); if (jc.isCancelled()) return null; if (bitmap != null) { bitmap = BitmapUtils.rotateBitmap(bitmap, mItem.getRotation() - mItem.getFullImageRotation(), true); } - return bitmap; + return new BitmapScreenNail(bitmap, mItem.getFullImageRotation()); } } @@ -604,16 +607,16 @@ public class PhotoDataAdapter implements PhotoPage.Model { } private class ScreenNailListener - implements Runnable, FutureListener { + implements Runnable, FutureListener { private final long mVersion; - private Future mFuture; + private Future mFuture; public ScreenNailListener(long version) { mVersion = version; } @Override - public void onFutureDone(Future future) { + public void onFutureDone(Future future) { mFuture = future; mMainHandler.sendMessage( mMainHandler.obtainMessage(MSG_RUN_OBJECT, this)); @@ -629,8 +632,8 @@ public class PhotoDataAdapter implements PhotoPage.Model { public int requestedBits = 0; public int rotation; public BitmapRegionDecoder fullImage; - public Bitmap screenNail; - public Future screenNailTask; + public ScreenNail screenNail; + public Future screenNailTask; public Future fullImageTask; public boolean failToLoad = false; } -- cgit v1.2.3