diff options
author | Owen Lin <owenlin@google.com> | 2011-08-17 22:07:43 +0800 |
---|---|---|
committer | Owen Lin <owenlin@google.com> | 2011-08-18 13:33:50 +0800 |
commit | a2fba687d4d2dbb3b2db8866b054ecb0e42871b2 (patch) | |
tree | dacc5a60ed945fe989aebf1f227f72bc90ebc4b8 /src/com/android/gallery3d/app/SinglePhotoDataAdapter.java | |
parent | a053a3179cfee3d2bb666eff5f4f03a96b092e04 (diff) | |
download | android_packages_apps_Snap-a2fba687d4d2dbb3b2db8866b054ecb0e42871b2.tar.gz android_packages_apps_Snap-a2fba687d4d2dbb3b2db8866b054ecb0e42871b2.tar.bz2 android_packages_apps_Snap-a2fba687d4d2dbb3b2db8866b054ecb0e42871b2.zip |
Initial code for Gallery2.
fix: 5176434
Change-Id: I041e282b9c7b34ceb1db8b033be2b853bb3a992c
Diffstat (limited to 'src/com/android/gallery3d/app/SinglePhotoDataAdapter.java')
-rw-r--r-- | src/com/android/gallery3d/app/SinglePhotoDataAdapter.java | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/src/com/android/gallery3d/app/SinglePhotoDataAdapter.java b/src/com/android/gallery3d/app/SinglePhotoDataAdapter.java new file mode 100644 index 000000000..11e0013cc --- /dev/null +++ b/src/com/android/gallery3d/app/SinglePhotoDataAdapter.java @@ -0,0 +1,181 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * 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.android.gallery3d.app; + +import com.android.gallery3d.common.BitmapUtils; +import com.android.gallery3d.common.Utils; +import com.android.gallery3d.data.MediaItem; +import com.android.gallery3d.data.Path; +import com.android.gallery3d.ui.PhotoView; +import com.android.gallery3d.ui.PhotoView.ImageData; +import com.android.gallery3d.ui.SynchronizedHandler; +import com.android.gallery3d.ui.TileImageViewAdapter; +import com.android.gallery3d.util.Future; +import com.android.gallery3d.util.FutureListener; +import com.android.gallery3d.util.ThreadPool; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.BitmapRegionDecoder; +import android.graphics.Rect; +import android.os.Handler; +import android.os.Message; + +public class SinglePhotoDataAdapter extends TileImageViewAdapter + implements PhotoPage.Model { + + private static final String TAG = "SinglePhotoDataAdapter"; + private static final int SIZE_BACKUP = 640; + private static final int MSG_UPDATE_IMAGE = 1; + + private MediaItem mItem; + private boolean mHasFullImage; + private Future<?> mTask; + private BitmapRegionDecoder mDecoder; + private Bitmap mBackup; + private Handler mHandler; + + private PhotoView mPhotoView; + private ThreadPool mThreadPool; + + public SinglePhotoDataAdapter( + GalleryActivity activity, PhotoView view, MediaItem item) { + mItem = Utils.checkNotNull(item); + mHasFullImage = (item.getSupportedOperations() & + MediaItem.SUPPORT_FULL_IMAGE) != 0; + mPhotoView = Utils.checkNotNull(view); + mHandler = new SynchronizedHandler(activity.getGLRoot()) { + @Override + @SuppressWarnings("unchecked") + public void handleMessage(Message message) { + Utils.assertTrue(message.what == MSG_UPDATE_IMAGE); + if (mHasFullImage) { + onDecodeLargeComplete((Future<BitmapRegionDecoder>) + message.obj); + } else { + onDecodeThumbComplete((Future<Bitmap>) message.obj); + } + } + }; + mThreadPool = activity.getThreadPool(); + } + + private FutureListener<BitmapRegionDecoder> mLargeListener = + new FutureListener<BitmapRegionDecoder>() { + public void onFutureDone(Future<BitmapRegionDecoder> future) { + mHandler.sendMessage( + mHandler.obtainMessage(MSG_UPDATE_IMAGE, future)); + } + }; + + private FutureListener<Bitmap> mThumbListener = + new FutureListener<Bitmap>() { + public void onFutureDone(Future<Bitmap> future) { + mHandler.sendMessage( + mHandler.obtainMessage(MSG_UPDATE_IMAGE, future)); + } + }; + + public boolean isEmpty() { + return false; + } + + public int getImageRotation() { + return mItem.getRotation(); + } + + private void onDecodeLargeComplete(Future<BitmapRegionDecoder> future) { + try { + mDecoder = future.get(); + if (mDecoder == null) return; + int width = mDecoder.getWidth(); + int height = mDecoder.getHeight(); + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inSampleSize = BitmapUtils.computeSampleSizeLarger( + width, height, SIZE_BACKUP); + mBackup = mDecoder.decodeRegion( + new Rect(0, 0, width, height), options); + setBackupImage(mBackup, width, height); + setRegionDecoder(mDecoder); + mPhotoView.notifyImageInvalidated(0); + } catch (Throwable t) { + Log.w(TAG, "fail to decode large", t); + } + } + + private void onDecodeThumbComplete(Future<Bitmap> future) { + try { + mBackup = future.get(); + if (mBackup == null) return; + setBackupImage(mBackup, mBackup.getWidth(), mBackup.getHeight()); + mPhotoView.notifyOnNewImage(); + mPhotoView.notifyImageInvalidated(0); // the current image + } catch (Throwable t) { + Log.w(TAG, "fail to decode thumb", t); + } + } + + public void resume() { + if (mTask == null) { + if (mHasFullImage) { + mTask = mThreadPool.submit( + mItem.requestLargeImage(), mLargeListener); + } else { + mTask = mThreadPool.submit( + mItem.requestImage(MediaItem.TYPE_THUMBNAIL), + mThumbListener); + } + } + } + + public void pause() { + Future<?> task = mTask; + task.cancel(); + task.waitDone(); + if (task.get() == null) { + mTask = null; + } + } + + public ImageData getNextImage() { + return null; + } + + public ImageData getPreviousImage() { + return null; + } + + public void next() { + throw new UnsupportedOperationException(); + } + + public void previous() { + throw new UnsupportedOperationException(); + } + + public MediaItem getCurrentMediaItem() { + return mItem; + } + + public int getCurrentIndex() { + return 0; + } + + public void setCurrentPhoto(Path path, int indexHint) { + // ignore + } +} |