diff options
author | Chih-Chung Chang <chihchung@google.com> | 2012-05-11 17:20:53 +0800 |
---|---|---|
committer | Chih-Chung Chang <chihchung@google.com> | 2012-05-11 17:30:47 +0800 |
commit | c31bda66b05eae587fd86f758e2bd7ce30c58394 (patch) | |
tree | 1097b02ccd28c0e3610c8039223ebedc847d2856 | |
parent | 34e6b587229c4863bb805859d9515abecd8abc5d (diff) | |
download | android_packages_apps_Snap-c31bda66b05eae587fd86f758e2bd7ce30c58394.tar.gz android_packages_apps_Snap-c31bda66b05eae587fd86f758e2bd7ce30c58394.tar.bz2 android_packages_apps_Snap-c31bda66b05eae587fd86f758e2bd7ce30c58394.zip |
Better data change handling for PhotoDataAdapter and PhotoView.
Change-Id: I0f67d918b95000d10786d2035286e036346bfa4e
-rw-r--r-- | src/com/android/gallery3d/app/PhotoDataAdapter.java | 81 | ||||
-rw-r--r-- | src/com/android/gallery3d/data/LocalImage.java | 11 | ||||
-rw-r--r-- | src/com/android/gallery3d/ui/PhotoView.java | 43 |
3 files changed, 76 insertions, 59 deletions
diff --git a/src/com/android/gallery3d/app/PhotoDataAdapter.java b/src/com/android/gallery3d/app/PhotoDataAdapter.java index df17a6cdb..d88b72ca8 100644 --- a/src/com/android/gallery3d/app/PhotoDataAdapter.java +++ b/src/com/android/gallery3d/app/PhotoDataAdapter.java @@ -128,10 +128,13 @@ public class PhotoDataAdapter implements PhotoPage.Model { // mCurrentIndex triggers the data loading and image loading. private int mCurrentIndex; - // mChanges keeps the version number (of MediaItem) about the previous, - // current, and next image. If the version number changes, we notify the - // view. This is used after a database reload or mCurrentIndex changes. + // mChanges keeps the version number (of MediaItem) about the images. If any + // of the version number changes, we notify the view. This is used after a + // database reload or mCurrentIndex changes. private final long mChanges[] = new long[IMAGE_CACHE_SIZE]; + // mPaths keeps the corresponding Path (of MediaItem) for the images. This + // is used to determine the item movement. + private final Path mPaths[] = new Path[IMAGE_CACHE_SIZE]; private final Handler mMainHandler; private final ThreadPool mThreadPool; @@ -158,7 +161,8 @@ public class PhotoDataAdapter implements PhotoPage.Model { // The path of the current viewing item will be stored in mItemPath. // If mItemPath is not null, mCurrentIndex is only a hint for where we // can find the item. If mItemPath is null, then we use the mCurrentIndex to - // find the image being viewed. + // find the image being viewed. cameraIndex is the index of the camera + // preview. If cameraIndex < 0, there is no camera preview. public PhotoDataAdapter(GalleryActivity activity, PhotoView view, MediaSet mediaSet, Path itemPath, int indexHint, int cameraIndex) { mSource = Utils.checkNotNull(mediaSet); @@ -199,20 +203,73 @@ public class PhotoDataAdapter implements PhotoPage.Model { updateSlidingWindow(); } - private long getVersion(int index) { - if (index < 0 || index >= mSize) return MediaObject.INVALID_DATA_VERSION; + private MediaItem getItemInternal(int index) { + if (index < 0 || index >= mSize) return null; if (index >= mContentStart && index < mContentEnd) { - MediaItem item = mData[index % DATA_CACHE_SIZE]; - if (item != null) return item.getDataVersion(); + return mData[index % DATA_CACHE_SIZE]; } - return MediaObject.INVALID_DATA_VERSION; + return null; + } + + private long getVersion(int index) { + MediaItem item = getItemInternal(index); + if (item == null) return MediaObject.INVALID_DATA_VERSION; + return item.getDataVersion(); + } + + private Path getPath(int index) { + MediaItem item = getItemInternal(index); + if (item == null) return null; + return item.getPath(); } private void fireDataChange() { + // First check if data actually changed. + boolean changed = false; for (int i = -SCREEN_NAIL_MAX; i <= SCREEN_NAIL_MAX; ++i) { - mChanges[i + SCREEN_NAIL_MAX] = getVersion(mCurrentIndex + i); + long newVersion = getVersion(mCurrentIndex + i); + if (mChanges[i + SCREEN_NAIL_MAX] != newVersion) { + mChanges[i + SCREEN_NAIL_MAX] = newVersion; + changed = true; + } } - mPhotoView.notifyDataChange(mChanges, -mCurrentIndex, + + if (!changed) return; + + // Now calculate the fromIndex array. fromIndex represents the item + // movement. It records the index where the picture come from. The + // special value Integer.MAX_VALUE means it's a new picture. + final int N = IMAGE_CACHE_SIZE; + int fromIndex[] = new int[N]; + + // Remember the old path array. + Path oldPaths[] = new Path[N]; + System.arraycopy(mPaths, 0, oldPaths, 0, N); + + // Update the mPaths array. + for (int i = 0; i < N; ++i) { + mPaths[i] = getPath(mCurrentIndex + i - SCREEN_NAIL_MAX); + } + + // Calculate the fromIndex array. + for (int i = 0; i < N; i++) { + Path p = mPaths[i]; + if (p == null) { + fromIndex[i] = Integer.MAX_VALUE; + continue; + } + + // Try to find the same path in the old array + int j; + for (j = 0; j < N; j++) { + if (oldPaths[j] == p) { + break; + } + } + fromIndex[i] = (j < N) ? j - SCREEN_NAIL_MAX : Integer.MAX_VALUE; + } + + mPhotoView.notifyDataChange(fromIndex, -mCurrentIndex, mSize - 1 - mCurrentIndex); } @@ -542,7 +599,7 @@ public class PhotoDataAdapter implements PhotoPage.Model { bitmap = BitmapUtils.rotateBitmap(bitmap, mItem.getRotation() - mItem.getFullImageRotation(), true); } - return new BitmapScreenNail(bitmap); + return bitmap == null ? null : new BitmapScreenNail(bitmap); } } diff --git a/src/com/android/gallery3d/data/LocalImage.java b/src/com/android/gallery3d/data/LocalImage.java index 4f2797e02..f96aca3e3 100644 --- a/src/com/android/gallery3d/data/LocalImage.java +++ b/src/com/android/gallery3d/data/LocalImage.java @@ -57,7 +57,7 @@ public class LocalImage extends LocalMediaItem { private static final int INDEX_DATA = 8; private static final int INDEX_ORIENTATION = 9; private static final int INDEX_BUCKET_ID = 10; - private static final int INDEX_SIZE_ID = 11; + private static final int INDEX_SIZE = 11; private static final int INDEX_WIDTH = 12; private static final int INDEX_HEIGHT = 13; @@ -74,9 +74,8 @@ public class LocalImage extends LocalMediaItem { ImageColumns.ORIENTATION, // 9 ImageColumns.BUCKET_ID, // 10 ImageColumns.SIZE, // 11 - // These should be changed to proper names after they are made public. - "width", // ImageColumns.WIDTH, // 12 - "height", // ImageColumns.HEIGHT // 13 + ImageColumns.WIDTH, // 12 + ImageColumns.HEIGHT // 13 }; private final GalleryApp mApplication; @@ -121,7 +120,7 @@ public class LocalImage extends LocalMediaItem { filePath = cursor.getString(INDEX_DATA); rotation = cursor.getInt(INDEX_ORIENTATION); bucketId = cursor.getInt(INDEX_BUCKET_ID); - fileSize = cursor.getLong(INDEX_SIZE_ID); + fileSize = cursor.getLong(INDEX_SIZE); width = cursor.getInt(INDEX_WIDTH); height = cursor.getInt(INDEX_HEIGHT); } @@ -143,7 +142,7 @@ public class LocalImage extends LocalMediaItem { filePath = uh.update(filePath, cursor.getString(INDEX_DATA)); rotation = uh.update(rotation, cursor.getInt(INDEX_ORIENTATION)); bucketId = uh.update(bucketId, cursor.getInt(INDEX_BUCKET_ID)); - fileSize = uh.update(fileSize, cursor.getLong(INDEX_SIZE_ID)); + fileSize = uh.update(fileSize, cursor.getLong(INDEX_SIZE)); width = uh.update(width, cursor.getInt(INDEX_WIDTH)); height = uh.update(height, cursor.getInt(INDEX_HEIGHT)); return uh.isUpdated(); diff --git a/src/com/android/gallery3d/ui/PhotoView.java b/src/com/android/gallery3d/ui/PhotoView.java index cd11b8e27..dce37289e 100644 --- a/src/com/android/gallery3d/ui/PhotoView.java +++ b/src/com/android/gallery3d/ui/PhotoView.java @@ -142,9 +142,6 @@ public class PhotoView extends GLView { private final RangeArray<Picture> mPictures = new RangeArray<Picture>(-SCREEN_NAIL_MAX, SCREEN_NAIL_MAX); - private final long mDataVersion[] = new long[2 * SCREEN_NAIL_MAX + 1]; - private final int mFromIndex[] = new int[2 * SCREEN_NAIL_MAX + 1]; - private final MyGestureListener mGestureListener; private final GestureRecognizer mGestureRecognizer; private final PositionController mPositionController; @@ -224,7 +221,6 @@ public class PhotoView extends GLView { } }); mVideoPlayIcon = new ResourceTexture(context, R.drawable.ic_control_play); - Arrays.fill(mDataVersion, INVALID_DATA_VERSION); for (int i = -SCREEN_NAIL_MAX; i <= SCREEN_NAIL_MAX; i++) { if (i == 0) { mPictures.put(i, new FullPicture()); @@ -305,47 +301,12 @@ public class PhotoView extends GLView { // Data/Image change notifications //////////////////////////////////////////////////////////////////////////// - public void notifyDataChange(long[] versions, int prevBound, int nextBound) { + public void notifyDataChange(int[] fromIndex, int prevBound, int nextBound) { mPrevBound = prevBound; mNextBound = nextBound; - // Check if the data version actually changed. - boolean changed = false; - int N = 2 * SCREEN_NAIL_MAX + 1; - for (int i = 0; i < N; i++) { - if (versions[i] != mDataVersion[i]) { - changed = true; - break; - } - } - if (!changed) return; - - // Create the mFromIndex array, which records the index where the picture - // come from. The value Integer.MAX_VALUE means it's a new picture. - for (int i = 0; i < N; i++) { - long v = versions[i]; - if (v == INVALID_DATA_VERSION) { - mFromIndex[i] = Integer.MAX_VALUE; - continue; - } - - // Try to find the same version number in the old array - int j; - for (j = 0; j < N; j++) { - if (mDataVersion[j] == v) { - break; - } - } - mFromIndex[i] = (j < N) ? j - SCREEN_NAIL_MAX : Integer.MAX_VALUE; - } - - // Copy the new data version - for (int i = 0; i < N; i++) { - mDataVersion[i] = versions[i]; - } - // Move the boxes - mPositionController.moveBox(mFromIndex, mPrevBound < 0, mNextBound > 0, + mPositionController.moveBox(fromIndex, mPrevBound < 0, mNextBound > 0, mModel.isCamera(0)); // Update the ScreenNails. |