OSDN Git Service

Prevent album's label get flashing when content changed.
authorOwen Lin <owenlin@google.com>
Mon, 9 Apr 2012 08:11:30 +0000 (16:11 +0800)
committerOwen Lin <owenlin@google.com>
Tue, 10 Apr 2012 03:12:58 +0000 (11:12 +0800)
Change-Id: I1b00b308af740ccab143a073643729ca880770f4

src/com/android/gallery3d/app/AlbumSetDataLoader.java
src/com/android/gallery3d/ui/AlbumLabelMaker.java
src/com/android/gallery3d/ui/AlbumSetSlidingWindow.java

index 1d3ab71..819adcc 100644 (file)
@@ -53,6 +53,7 @@ public class AlbumSetDataLoader {
 
     private final MediaSet[] mData;
     private final MediaItem[] mCoverItem;
+    private final int[] mTotalCount;
     private final long[] mItemVersion;
     private final long[] mSetVersion;
 
@@ -78,6 +79,7 @@ public class AlbumSetDataLoader {
         mSource = Utils.checkNotNull(albumSet);
         mCoverItem = new MediaItem[cacheSize];
         mData = new MediaSet[cacheSize];
+        mTotalCount = new int[cacheSize];
         mItemVersion = new long[cacheSize];
         mSetVersion = new long[cacheSize];
         Arrays.fill(mItemVersion, MediaObject.INVALID_DATA_VERSION);
@@ -113,22 +115,28 @@ public class AlbumSetDataLoader {
         mReloadTask.start();
     }
 
-    public MediaSet getMediaSet(int index) {
+    private void assertIsActive(int index) {
         if (index < mActiveStart && index >= mActiveEnd) {
             throw new IllegalArgumentException(String.format(
                     "%s not in (%s, %s)", index, mActiveStart, mActiveEnd));
         }
+    }
+
+    public MediaSet getMediaSet(int index) {
+        assertIsActive(index);
         return mData[index % mData.length];
     }
 
     public MediaItem getCoverItem(int index) {
-        if (index < mActiveStart && index >= mActiveEnd) {
-            throw new IllegalArgumentException(String.format(
-                    "%s not in (%s, %s)", index, mActiveStart, mActiveEnd));
-        }
+        assertIsActive(index);
         return mCoverItem[index % mCoverItem.length];
     }
 
+    public int getTotalCount(int index) {
+        assertIsActive(index);
+        return mTotalCount[index % mTotalCount.length];
+    }
+
     public int getActiveStart() {
         return mActiveStart;
     }
@@ -144,6 +152,7 @@ public class AlbumSetDataLoader {
     private void clearSlot(int slotIndex) {
         mData[slotIndex] = null;
         mCoverItem[slotIndex] = null;
+        mTotalCount[slotIndex] = 0;
         mItemVersion[slotIndex] = MediaObject.INVALID_DATA_VERSION;
         mSetVersion[slotIndex] = MediaObject.INVALID_DATA_VERSION;
     }
@@ -216,6 +225,7 @@ public class AlbumSetDataLoader {
         public int size;
         public MediaSet item;
         public MediaItem cover;
+        public int totalCount;
     }
 
     private class GetUpdateInfo implements Callable<UpdateInfo> {
@@ -276,6 +286,7 @@ public class AlbumSetDataLoader {
                 mItemVersion[pos] = itemVersion;
                 mData[pos] = info.item;
                 mCoverItem[pos] = info.cover;
+                mTotalCount[pos] = info.totalCount;
                 if (mDataListener != null
                         && info.index >= mActiveStart && info.index < mActiveEnd) {
                     mDataListener.onContentChanged(info.index);
@@ -356,6 +367,7 @@ public class AlbumSetDataLoader {
                         info.item = mSource.getSubMediaSet(info.index);
                         if (info.item == null) continue;
                         info.cover = info.item.getCoverMediaItem();
+                        info.totalCount = info.item.getTotalMediaItemCount();
                     }
                 }
                 executeAndWait(new UpdateContent(info));
index 9fdc234..09c779b 100644 (file)
@@ -14,9 +14,7 @@ import android.text.TextPaint;
 import android.text.TextUtils;
 
 import com.android.gallery3d.R;
-import com.android.gallery3d.common.Utils;
 import com.android.gallery3d.data.DataSourceType;
-import com.android.gallery3d.data.MediaSet;
 import com.android.gallery3d.util.ThreadPool;
 import com.android.gallery3d.util.ThreadPool.JobContext;
 
@@ -112,15 +110,10 @@ public class AlbumLabelMaker {
 
     public ThreadPool.Job<Bitmap> requestLabel(
             String title, String count, int sourceType) {
-        return new AlbumLabelJob(null, title, count, sourceType);
+        return new AlbumLabelJob(title, count, sourceType);
     }
 
-    public ThreadPool.Job<Bitmap> requestLabel(
-            MediaSet album, int sourceType) {
-        return new AlbumLabelJob(album, null, null, sourceType);
-    }
-
-    private static void drawText(Canvas canvas,
+    static void drawText(Canvas canvas,
             int x, int y, String text, int lengthLimit, TextPaint p) {
         // The TextPaint cannot be used concurrently
         synchronized (p) {
@@ -131,14 +124,11 @@ public class AlbumLabelMaker {
     }
 
     private class AlbumLabelJob implements ThreadPool.Job<Bitmap> {
-        private final MediaSet mAlbum;
         private final String mTitle;
         private final String mCount;
         private final int mSourceType;
 
-        public AlbumLabelJob(MediaSet album,
-                String title, String count, int sourceType) {
-            mAlbum = album;
+        public AlbumLabelJob(String title, String count, int sourceType) {
             mTitle = title;
             mCount = count;
             mSourceType = sourceType;
@@ -146,14 +136,10 @@ public class AlbumLabelMaker {
 
         @Override
         public Bitmap run(JobContext jc) {
-            MediaSet album = mAlbum;
             AlbumSetSlotRenderer.LabelSpec s = mSpec;
 
-            String title = Utils.ensureNotNull(
-                    (album == null) ? mTitle : album.getName());
-            String count = album == null
-                    ? Utils.ensureNotNull(mCount)
-                    : String.valueOf(album.getTotalMediaItemCount());
+            String title = mTitle;
+            String count = mCount;
             Bitmap icon = getOverlayAlbumIcon(mSourceType);
 
             Bitmap bitmap;
index 5105c9b..54f1ec1 100644 (file)
@@ -1,4 +1,4 @@
-/*
+/*T
  * Copyright (C) 2010 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -72,6 +72,8 @@ public class AlbumSetSlidingWindow implements AlbumSetDataLoader.DataListener {
         public Texture content;
         public Texture label;
         public Path setPath;
+        public String title;
+        public int totalCount;
         public int sourceType;
         public int cacheFlag;
         public int cacheStatus;
@@ -227,23 +229,39 @@ public class AlbumSetSlidingWindow implements AlbumSetDataLoader.DataListener {
         mData[slotIndex % mData.length] = null;
     }
 
-    private void updateAlbumSetEntry(AlbumSetEntry entry,
-            int slotIndex, MediaSet album, MediaItem cover) {
+    private boolean isLabelChanged(
+            AlbumSetEntry entry, String title, int totalCount, int sourceType) {
+        return !Utils.equals(entry.title, title)
+                || entry.totalCount != totalCount
+                || entry.sourceType != sourceType;
+    }
+
+    private void updateAlbumSetEntry(AlbumSetEntry entry, int slotIndex) {
+        MediaSet album = mSource.getMediaSet(slotIndex);
+        MediaItem cover = mSource.getCoverItem(slotIndex);
+        int totalCount = mSource.getTotalCount(slotIndex);
+
         entry.album = album;
         entry.setDataVersion = getDataVersion(album);
-        entry.sourceType = DataSourceType.identifySourceType(album);
         entry.cacheFlag = identifyCacheFlag(album);
         entry.cacheStatus = identifyCacheStatus(album);
         entry.setPath = (album == null) ? null : album.getPath();
 
-        if (entry.labelLoader != null) {
-            entry.labelLoader.recycle();
-            entry.labelLoader = null;
-            entry.label = null;
-        }
-        if (album != null) {
-            entry.labelLoader =
-                    new AlbumLabelLoader(slotIndex, album, entry.sourceType);
+        String title = (album == null) ? "" : Utils.ensureNotNull(album.getName());
+        int sourceType = DataSourceType.identifySourceType(album);
+        if (isLabelChanged(entry, title, totalCount, sourceType)) {
+            entry.title = title;
+            entry.totalCount = totalCount;
+            entry.sourceType = sourceType;
+            if (entry.labelLoader != null) {
+                entry.labelLoader.recycle();
+                entry.labelLoader = null;
+                entry.label = null;
+            }
+            if (album != null) {
+                entry.labelLoader = new AlbumLabelLoader(
+                        slotIndex, title, totalCount, sourceType);
+            }
         }
 
         entry.coverItem = cover;
@@ -264,10 +282,8 @@ public class AlbumSetSlidingWindow implements AlbumSetDataLoader.DataListener {
     }
 
     private void prepareSlotContent(int slotIndex) {
-        MediaSet set = mSource.getMediaSet(slotIndex);
-        MediaItem coverItem = mSource.getCoverItem(slotIndex);
         AlbumSetEntry entry = new AlbumSetEntry();
-        updateAlbumSetEntry(entry, slotIndex, set, coverItem);
+        updateAlbumSetEntry(entry, slotIndex);
         mData[slotIndex % mData.length] = entry;
     }
 
@@ -350,9 +366,7 @@ public class AlbumSetSlidingWindow implements AlbumSetDataLoader.DataListener {
         }
 
         AlbumSetEntry entry = mData[index % mData.length];
-        MediaSet set = mSource.getMediaSet(index);
-        MediaItem coverItem = mSource.getCoverItem(index);
-        updateAlbumSetEntry(entry, index, set, coverItem);
+        updateAlbumSetEntry(entry, index);
         updateAllImageRequests();
         updateTextureUploadQueue();
         if (mListener != null && isActiveSlot(index)) {
@@ -362,8 +376,8 @@ public class AlbumSetSlidingWindow implements AlbumSetDataLoader.DataListener {
 
     public BitmapTexture getLoadingTexture() {
         if (mLoadingLabel == null) {
-            Bitmap bitmap = mLabelMaker.requestLabel(mLoadingText, null,
-                    DataSourceType.TYPE_NOT_CATEGORIZED)
+            Bitmap bitmap = mLabelMaker.requestLabel(
+                    mLoadingText, "", DataSourceType.TYPE_NOT_CATEGORIZED)
                     .run(ThreadPool.JOB_CONTEXT_STUB);
             mLoadingLabel = new BitmapTexture(bitmap);
             mLoadingLabel.setOpaque(false);
@@ -455,21 +469,23 @@ public class AlbumSetSlidingWindow implements AlbumSetDataLoader.DataListener {
     }
 
     private class AlbumLabelLoader extends BitmapLoader implements EntryUpdater {
-        private final MediaSet mMediaSet;
         private final int mSlotIndex;
+        private final String mTitle;
+        private final int mTotalCount;
         private final int mSourceType;
 
         public AlbumLabelLoader(
-                int slotIndex, MediaSet mediaSet, int sourceType) {
+                int slotIndex, String title, int totalCount, int sourceType) {
             mSlotIndex = slotIndex;
-            mMediaSet = mediaSet;
+            mTitle = title;
+            mTotalCount = totalCount;
             mSourceType = sourceType;
         }
 
         @Override
         protected Future<Bitmap> submitBitmapTask(FutureListener<Bitmap> l) {
             return mThreadPool.submit(mLabelMaker.requestLabel(
-                    mMediaSet, mSourceType), l);
+                    mTitle, String.valueOf(mTotalCount), mSourceType), l);
         }
 
         @Override
@@ -519,9 +535,10 @@ public class AlbumSetSlidingWindow implements AlbumSetDataLoader.DataListener {
                 entry.labelLoader = null;
                 entry.label = null;
             }
-            entry.labelLoader = (entry.album == null)
-                    ? null
-                    : new AlbumLabelLoader(i, entry.album, entry.sourceType);
+            if (entry.album != null) {
+                entry.labelLoader = new AlbumLabelLoader(i,
+                        entry.title, entry.totalCount, entry.sourceType);
+            }
         }
         updateAllImageRequests();
         updateTextureUploadQueue();