MediaFeed.java revision 0e0a636482418d6836bb783d2b00e9e3706dfc14
1f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparkspackage com.cooliris.media; 2f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 3f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparksimport java.util.ArrayList; 4f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparksimport java.util.HashMap; 5897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnarajimport java.util.HashSet; 61b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkatimport java.util.Set; 7f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 8897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnarajimport android.content.ContentResolver; 9f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparksimport android.content.Context; 10897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnarajimport android.database.ContentObserver; 111c729acdafe147abda6b3479eafb17607f172780Venkatimport android.util.Log; 12f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparksimport android.view.Gravity; 13f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparksimport android.widget.Toast; 14897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnarajimport android.net.Uri; 15897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnarajimport android.os.Handler; 16f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparksimport android.os.Process; 17f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 18d93f177bf2bc26f4ad64ca2f7725da8b0347a77fVenkatimport com.cooliris.app.App; 1929dcedb79b7eb9e3d65adc5d2ea95c1dd5c01df9Chih-Chung Changimport com.cooliris.app.Res; 20f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparksimport com.cooliris.media.MediaClustering.Cluster; 21f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 22f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparkspublic final class MediaFeed implements Runnable { 231c729acdafe147abda6b3479eafb17607f172780Venkat private final String TAG = "MediaFeed"; 24f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public static final int OPERATION_DELETE = 0; 25f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public static final int OPERATION_ROTATE = 1; 26f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public static final int OPERATION_CROP = 2; 27f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 28f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private static final int NUM_ITEMS_LOOKAHEAD = 60; 290e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen private static final int NUM_INTERRUPT_RETRIES = 30; 300e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen private static final int JOIN_TIMEOUT = 50; 31f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 32f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private IndexRange mVisibleRange = new IndexRange(); 33f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private IndexRange mBufferedRange = new IndexRange(); 34f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private ArrayList<MediaSet> mMediaSets = new ArrayList<MediaSet>(); 35f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private Listener mListener; 36f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private DataSource mDataSource; 37f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private boolean mListenerNeedsUpdate = false; 38d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks private boolean mMediaFeedNeedsToRun = false; 39f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private MediaSet mSingleWrapper = new MediaSet(); 40f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private boolean mInClusteringMode = false; 41f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private HashMap<MediaSet, MediaClustering> mClusterSets = new HashMap<MediaSet, MediaClustering>(32); 42f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private int mExpandedMediaSetIndex = Shared.INVALID; 43f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private MediaFilter mMediaFilter; 44f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private MediaSet mMediaFilteredSet; 45f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private Context mContext; 46f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private Thread mDataSourceThread = null; 47f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private Thread mAlbumSourceThread = null; 48f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private boolean mListenerNeedsLayout; 49f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private boolean mWaitingForMediaScanner; 50f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private boolean mSingleImageMode; 51f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private boolean mLoading; 52897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj private HashMap<String, ContentObserver> mContentObservers = new HashMap<String, ContentObserver>(); 5362dd11281ef6e12049dd6798b184157e9583d7d4Venkat private ArrayList<String[]> mRequestedRefresh = new ArrayList<String[]>(); 540e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen private volatile boolean mIsShutdown = false; 55f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 56f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public interface Listener { 57f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public abstract void onFeedAboutToChange(MediaFeed feed); 5862dd11281ef6e12049dd6798b184157e9583d7d4Venkat 59f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public abstract void onFeedChanged(MediaFeed feed, boolean needsLayout); 60f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 61f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 62f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public MediaFeed(Context context, DataSource dataSource, Listener listener) { 63f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mContext = context; 64f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mListener = listener; 65f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mDataSource = dataSource; 66f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mSingleWrapper.setNumExpectedItems(1); 67f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mLoading = true; 68f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 69d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks 70f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void shutdown() { 710e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen mIsShutdown = true; 72f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mDataSourceThread != null) { 73f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mDataSource.shutdown(); 740e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen repeatShuttingDownThread(mDataSourceThread); 75f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mDataSourceThread = null; 76f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 77f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mAlbumSourceThread != null) { 780e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen repeatShuttingDownThread(mAlbumSourceThread); 79f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mAlbumSourceThread = null; 80f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 81f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int numSets = mMediaSets.size(); 82f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks for (int i = 0; i < numSets; ++i) { 83f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet set = mMediaSets.get(i); 84f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks set.clear(); 85f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 86f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks synchronized (mMediaSets) { 87f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mMediaSets.clear(); 88f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 89f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int numClusters = mClusterSets.size(); 90f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks for (int i = 0; i < numClusters; ++i) { 91f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaClustering mc = mClusterSets.get(i); 92f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mc != null) { 93f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mc.clear(); 94f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 95f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 96f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mClusterSets.clear(); 97f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mListener = null; 98f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mDataSource = null; 99f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mSingleWrapper = null; 100f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 101f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 1020e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen private void repeatShuttingDownThread(Thread targetThread) { 1030e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen for (int i = 0; i < NUM_INTERRUPT_RETRIES && targetThread.isAlive(); ++i) { 1040e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen targetThread.interrupt(); 1050e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen try { 1060e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen targetThread.join(JOIN_TIMEOUT); 1070e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen } catch (InterruptedException e) { 1080e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen Log.w(TAG, "Cannot stop the thread: " + targetThread.getName(), e); 1090e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen Thread.currentThread().interrupt(); 1100e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen return; 1110e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen } 1120e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen } 1130e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen 1140e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen if (targetThread.isAlive()) { 1150e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen Log.w(TAG, "Cannot stop the thread: " + targetThread.getName()); 1160e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen } 1170e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen } 1180e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen 119f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void setVisibleRange(int begin, int end) { 120d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks if (begin != mVisibleRange.begin || end != mVisibleRange.end) { 121d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mVisibleRange.begin = begin; 122d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mVisibleRange.end = end; 123d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks int numItems = 96; 124d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks int numItemsBy2 = numItems / 2; 125d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks int numItemsBy4 = numItems / 4; 126d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mBufferedRange.begin = (begin / numItemsBy2) * numItemsBy2 - numItemsBy4; 127d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mBufferedRange.end = mBufferedRange.begin + numItems; 128d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mMediaFeedNeedsToRun = true; 129d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks } 130f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 131f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 132f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void setFilter(MediaFilter filter) { 133f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mMediaFilter = filter; 134f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mMediaFilteredSet = null; 135f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mListener != null) { 136f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mListener.onFeedAboutToChange(this); 137f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 138d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mMediaFeedNeedsToRun = true; 139f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 140f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 141f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void removeFilter() { 142f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mMediaFilter = null; 143f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mMediaFilteredSet = null; 144f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mListener != null) { 145f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mListener.onFeedAboutToChange(this); 146f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks updateListener(true); 147f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 148d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mMediaFeedNeedsToRun = true; 149f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 150f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 151f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public ArrayList<MediaSet> getMediaSets() { 152f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return mMediaSets; 153f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 154f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 155d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks public MediaSet getMediaSet(final long setId) { 156f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (setId != Shared.INVALID) { 157d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks try { 158d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks int mMediaSetsSize = mMediaSets.size(); 159d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks for (int i = 0; i < mMediaSetsSize; i++) { 1601b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj final MediaSet set = mMediaSets.get(i); 1611b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj if (set.mId == setId) { 1621b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj set.mFlagForDelete = false; 1631b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj return set; 164d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks } 165f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 166d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks } catch (Exception e) { 167d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks return null; 168f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 169f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 170f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return null; 171f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 172f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 173f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public MediaSet getFilteredSet() { 174f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return mMediaFilteredSet; 175f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 176f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 177f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public MediaSet addMediaSet(final long setId, DataSource dataSource) { 178f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet mediaSet = new MediaSet(dataSource); 179f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mediaSet.mId = setId; 180f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mMediaSets.add(mediaSet); 181d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks if (mDataSourceThread != null && !mDataSourceThread.isAlive()) { 182d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mDataSourceThread.start(); 183d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks } 184d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mMediaFeedNeedsToRun = true; 185f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return mediaSet; 186f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 187f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 188f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public DataSource getDataSource() { 189f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return mDataSource; 190f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 191f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 192f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public MediaClustering getClustering() { 193f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mExpandedMediaSetIndex != Shared.INVALID && mExpandedMediaSetIndex < mMediaSets.size()) { 194f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return mClusterSets.get(mMediaSets.get(mExpandedMediaSetIndex)); 195f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 196f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return null; 197f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 198f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 199f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public ArrayList<Cluster> getClustersForSet(final MediaSet set) { 200f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks ArrayList<Cluster> clusters = null; 201f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mClusterSets != null && mClusterSets.containsKey(set)) { 202f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaClustering mediaClustering = mClusterSets.get(set); 203f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mediaClustering != null) { 204f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks clusters = mediaClustering.getClusters(); 205f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 206f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 207f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return clusters; 208f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 209f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 210f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void addItemToMediaSet(MediaItem item, MediaSet mediaSet) { 211f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks item.mParentMediaSet = mediaSet; 212f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mediaSet.addItem(item); 213d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks synchronized (mClusterSets) { 214f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (item.mClusteringState == MediaItem.NOT_CLUSTERED) { 215f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaClustering clustering = mClusterSets.get(mediaSet); 216f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (clustering == null) { 217f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks clustering = new MediaClustering(mediaSet.isPicassaAlbum()); 218f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mClusterSets.put(mediaSet, clustering); 219f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 220f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks clustering.setTimeRange(mediaSet.mMaxTimestamp - mediaSet.mMinTimestamp, mediaSet.getNumExpectedItems()); 221f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks clustering.addItemForClustering(item); 222f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks item.mClusteringState = MediaItem.CLUSTERED; 223f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 224f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 225d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mMediaFeedNeedsToRun = true; 226f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 227f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 228f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void performOperation(final int operation, final ArrayList<MediaBucket> mediaBuckets, final Object data) { 229f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int numBuckets = mediaBuckets.size(); 230f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks final ArrayList<MediaBucket> copyMediaBuckets = new ArrayList<MediaBucket>(numBuckets); 231f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks for (int i = 0; i < numBuckets; ++i) { 232f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks copyMediaBuckets.add(mediaBuckets.get(i)); 233f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 234f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (operation == OPERATION_DELETE && mListener != null) { 235f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mListener.onFeedAboutToChange(this); 236f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 237f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks Thread operationThread = new Thread(new Runnable() { 238f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void run() { 239f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks ArrayList<MediaBucket> mediaBuckets = copyMediaBuckets; 240f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (operation == OPERATION_DELETE) { 241f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int numBuckets = mediaBuckets.size(); 242f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks for (int i = 0; i < numBuckets; ++i) { 243f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaBucket bucket = mediaBuckets.get(i); 244f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet set = bucket.mediaSet; 245f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks ArrayList<MediaItem> items = bucket.mediaItems; 246f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (set != null && items == null) { 247f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks // Remove the entire bucket. 248f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks removeMediaSet(set); 249f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } else if (set != null && items != null) { 250f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks // We need to remove these items from the set. 251f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int numItems = items.size(); 2521bb0c42b2a62f580eea4764d6a4434ffecfbf353Dave Sparks // We also need to delete the items from the 2531bb0c42b2a62f580eea4764d6a4434ffecfbf353Dave Sparks // cluster. 254f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaClustering clustering = mClusterSets.get(set); 255f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks for (int j = 0; j < numItems; ++j) { 256f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaItem item = items.get(j); 257f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks removeItemFromMediaSet(item, set); 258f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (clustering != null) { 259f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks clustering.removeItemFromClustering(item); 260f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 261f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 262f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks set.updateNumExpectedItems(); 263f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks set.generateTitle(true); 264f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 265f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 266f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks updateListener(true); 267d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mMediaFeedNeedsToRun = true; 268f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mDataSource != null) { 269f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mDataSource.performOperation(OPERATION_DELETE, mediaBuckets, null); 270f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 271f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } else { 272f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mDataSource.performOperation(operation, mediaBuckets, data); 273f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 274f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 275f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks }); 276f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks operationThread.setName("Operation " + operation); 277f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks operationThread.start(); 278f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 279f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 280f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void removeMediaSet(MediaSet set) { 2812b04f591f758b7af03c43d90e91b939cbd515506Venkat Krishnaraj synchronized (mMediaSets) { 2821bb0c42b2a62f580eea4764d6a4434ffecfbf353Dave Sparks mMediaSets.remove(set); 2832b04f591f758b7af03c43d90e91b939cbd515506Venkat Krishnaraj } 284d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mMediaFeedNeedsToRun = true; 285f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 286f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 287f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private void removeItemFromMediaSet(MediaItem item, MediaSet mediaSet) { 288f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mediaSet.removeItem(item); 289d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks synchronized (mClusterSets) { 290f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaClustering clustering = mClusterSets.get(mediaSet); 291f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (clustering != null) { 292f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks clustering.removeItemFromClustering(item); 293f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 294f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 295d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mMediaFeedNeedsToRun = true; 296f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 297f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 298f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void updateListener(boolean needsLayout) { 299f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mListenerNeedsUpdate = true; 300f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mListenerNeedsLayout = needsLayout; 301f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 302f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 303f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public int getNumSlots() { 304f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int currentMediaSetIndex = mExpandedMediaSetIndex; 305f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks ArrayList<MediaSet> mediaSets = mMediaSets; 306f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int mediaSetsSize = mediaSets.size(); 307f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 308f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mInClusteringMode == false) { 309f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (currentMediaSetIndex == Shared.INVALID || currentMediaSetIndex >= mediaSetsSize) { 310f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return mediaSetsSize; 311f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } else { 312f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet setToUse = (mMediaFilteredSet == null) ? mediaSets.get(currentMediaSetIndex) : mMediaFilteredSet; 3135e0474e8e1942c41234eb251f948c2fbb7445b02Venkat Krishnaraj return setToUse.getNumExpectedItems(); 314f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 315f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } else if (currentMediaSetIndex != Shared.INVALID && currentMediaSetIndex < mediaSetsSize) { 316f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet set = mediaSets.get(currentMediaSetIndex); 317f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaClustering clustering = mClusterSets.get(set); 318f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (clustering != null) { 319f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return clustering.getClustersForDisplay().size(); 320f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 321f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 322f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return 0; 323f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 32462dd11281ef6e12049dd6798b184157e9583d7d4Venkat 3257cde3b179f4d12f53c2a1db1e4f903bd06b7011eWei-Ta Chen public void copySlotStateFrom(MediaFeed another) { 3267cde3b179f4d12f53c2a1db1e4f903bd06b7011eWei-Ta Chen mExpandedMediaSetIndex = another.mExpandedMediaSetIndex; 3277cde3b179f4d12f53c2a1db1e4f903bd06b7011eWei-Ta Chen mInClusteringMode = another.mInClusteringMode; 3287cde3b179f4d12f53c2a1db1e4f903bd06b7011eWei-Ta Chen } 3297cde3b179f4d12f53c2a1db1e4f903bd06b7011eWei-Ta Chen 33088dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat public ArrayList<Integer> getBreaks() { 33188dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat if (true) 33288dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat return null; 33388dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat int currentMediaSetIndex = mExpandedMediaSetIndex; 33488dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat ArrayList<MediaSet> mediaSets = mMediaSets; 33588dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat int mediaSetsSize = mediaSets.size(); 33688dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat if (currentMediaSetIndex == Shared.INVALID || currentMediaSetIndex >= mediaSetsSize) 33788dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat return null; 33888dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat MediaSet set = mediaSets.get(currentMediaSetIndex); 33988dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat MediaClustering clustering = mClusterSets.get(set); 34088dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat if (clustering != null) { 34188dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat clustering.compute(null, true); 34288dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat final ArrayList<Cluster> clusters = clustering.getClustersForDisplay(); 34388dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat int numClusters = clusters.size(); 34488dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat final ArrayList<Integer> retVal = new ArrayList<Integer>(numClusters); 34588dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat int size = 0; 34688dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat for (int i = 0; i < numClusters; ++i) { 34788dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat size += clusters.get(i).getItems().size(); 34888dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat retVal.add(size); 34988dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat } 35088dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat return retVal; 35188dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat } else { 35288dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat return null; 35388dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat } 35488dc4ec391fdd373821aa94ae93ca0c231eb98bbVenkat } 355f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 356f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public MediaSet getSetForSlot(int slotIndex) { 357f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (slotIndex < 0) { 358f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return null; 359f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 360f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 361f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks ArrayList<MediaSet> mediaSets = mMediaSets; 362f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int mediaSetsSize = mediaSets.size(); 363f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int currentMediaSetIndex = mExpandedMediaSetIndex; 364f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 365f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mInClusteringMode == false) { 366f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (currentMediaSetIndex == Shared.INVALID || currentMediaSetIndex >= mediaSetsSize) { 367f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (slotIndex >= mediaSetsSize) { 368f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return null; 369f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 370f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return mMediaSets.get(slotIndex); 371f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 372f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mSingleWrapper.getNumItems() == 0) { 373f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mSingleWrapper.addItem(null); 374f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 375f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet setToUse = (mMediaFilteredSet == null) ? mMediaSets.get(currentMediaSetIndex) : mMediaFilteredSet; 376f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks ArrayList<MediaItem> items = setToUse.getItems(); 377f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (slotIndex >= setToUse.getNumItems()) { 378f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return null; 379f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 380f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mSingleWrapper.getItems().set(0, items.get(slotIndex)); 381f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return mSingleWrapper; 382f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } else if (currentMediaSetIndex != Shared.INVALID && currentMediaSetIndex < mediaSetsSize) { 383f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet set = mediaSets.get(currentMediaSetIndex); 384f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaClustering clustering = mClusterSets.get(set); 385f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (clustering != null) { 386f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks ArrayList<MediaClustering.Cluster> clusters = clustering.getClustersForDisplay(); 387f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (clusters.size() > slotIndex) { 388f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaClustering.Cluster cluster = clusters.get(slotIndex); 389f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks cluster.generateCaption(mContext); 390f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return cluster; 391f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 392f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 393f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 394f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return null; 395f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 396f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 397f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public boolean getWaitingForMediaScanner() { 398f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return mWaitingForMediaScanner; 399f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 400d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks 401f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public boolean isLoading() { 402f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return mLoading; 403f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 404f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 405f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void start() { 406f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks final MediaFeed feed = this; 40762f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat onResume(); 408f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mLoading = true; 409d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mDataSourceThread = new Thread(this); 410d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mDataSourceThread.setName("MediaFeed"); 4110e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen mIsShutdown = false; 412f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mAlbumSourceThread = new Thread(new Runnable() { 413f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void run() { 414f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mContext == null) 415f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return; 416f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 417f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks DataSource dataSource = mDataSource; 4181bb0c42b2a62f580eea4764d6a4434ffecfbf353Dave Sparks // We must wait while the SD card is mounted or the MediaScanner 4191bb0c42b2a62f580eea4764d6a4434ffecfbf353Dave Sparks // is running. 420d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks if (dataSource != null) { 4211b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj loadMediaSets(); 422d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks } 423f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mWaitingForMediaScanner = false; 424f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks while (ImageManager.isMediaScannerScanning(mContext.getContentResolver())) { 425f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks // MediaScanner is still running, wait 42688ed7e72a2a0bfdbef7f72aea2b2ddfd74961bdbVenkat Krishnaraj if (Thread.interrupted()) 42788ed7e72a2a0bfdbef7f72aea2b2ddfd74961bdbVenkat Krishnaraj return; 428f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mWaitingForMediaScanner = true; 429f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks try { 430f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mContext == null) 431f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return; 43229dcedb79b7eb9e3d65adc5d2ea95c1dd5c01df9Chih-Chung Chang showToast(mContext.getResources().getString(Res.string.initializing), Toast.LENGTH_LONG); 4331b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj if (dataSource != null) { 4341b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj loadMediaSets(); 4351b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj } 4361b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj Thread.sleep(10000); 437f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } catch (InterruptedException e) { 43888ed7e72a2a0bfdbef7f72aea2b2ddfd74961bdbVenkat Krishnaraj return; 439f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 440f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 441f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mWaitingForMediaScanner) { 44229dcedb79b7eb9e3d65adc5d2ea95c1dd5c01df9Chih-Chung Chang showToast(mContext.getResources().getString(Res.string.loading_new), Toast.LENGTH_LONG); 443d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mWaitingForMediaScanner = false; 4441b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj loadMediaSets(); 445f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 446f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mLoading = false; 447f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 448f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks }); 449f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mAlbumSourceThread.setName("MediaSets"); 450f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mAlbumSourceThread.start(); 451f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 452897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj 4531b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj private void loadMediaSets() { 4541b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj if (mDataSource == null) 4551b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj return; 4561b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj final ArrayList<MediaSet> sets = mMediaSets; 45762f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat synchronized (sets) { 45880182dccb216689e392da1c749f32f6c0cda6f42Venkat final int numSets = sets.size(); 45980182dccb216689e392da1c749f32f6c0cda6f42Venkat for (int i = 0; i < numSets; ++i) { 46080182dccb216689e392da1c749f32f6c0cda6f42Venkat final MediaSet set = sets.get(i); 46180182dccb216689e392da1c749f32f6c0cda6f42Venkat set.mFlagForDelete = true; 46280182dccb216689e392da1c749f32f6c0cda6f42Venkat } 46362f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat mDataSource.refresh(MediaFeed.this, mDataSource.getDatabaseUris()); 46462f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat mDataSource.loadMediaSets(MediaFeed.this); 46580182dccb216689e392da1c749f32f6c0cda6f42Venkat final ArrayList<MediaSet> setsToRemove = new ArrayList<MediaSet>(); 46680182dccb216689e392da1c749f32f6c0cda6f42Venkat for (int i = 0; i < numSets; ++i) { 46780182dccb216689e392da1c749f32f6c0cda6f42Venkat final MediaSet set = sets.get(i); 46880182dccb216689e392da1c749f32f6c0cda6f42Venkat if (set.mFlagForDelete) { 46980182dccb216689e392da1c749f32f6c0cda6f42Venkat setsToRemove.add(set); 47080182dccb216689e392da1c749f32f6c0cda6f42Venkat } 4711b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj } 47280182dccb216689e392da1c749f32f6c0cda6f42Venkat int numSetsToRemove = setsToRemove.size(); 47380182dccb216689e392da1c749f32f6c0cda6f42Venkat for (int i = 0; i < numSetsToRemove; ++i) { 47480182dccb216689e392da1c749f32f6c0cda6f42Venkat sets.remove(setsToRemove.get(i)); 47580182dccb216689e392da1c749f32f6c0cda6f42Venkat } 47680182dccb216689e392da1c749f32f6c0cda6f42Venkat setsToRemove.clear(); 4771b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj } 47880182dccb216689e392da1c749f32f6c0cda6f42Venkat mMediaFeedNeedsToRun = true; 4791b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj updateListener(false); 4801b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj } 481f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 482f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private void showToast(final String string, final int duration) { 483f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks showToast(string, duration, false); 484f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 485f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 486f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private void showToast(final String string, final int duration, final boolean centered) { 487d93f177bf2bc26f4ad64ca2f7725da8b0347a77fVenkat if (mContext != null && !App.get(mContext).isPaused()) { 48862f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat App.get(mContext).getHandler().post(new Runnable() { 489f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void run() { 490f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mContext != null) { 491f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks Toast toast = Toast.makeText(mContext, string, duration); 492f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (centered) { 493f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks toast.setGravity(Gravity.CENTER, 0, 0); 494f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 495f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks toast.show(); 496f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 497f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 498f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks }); 499f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 500f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 501f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 502f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void run() { 503f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks DataSource dataSource = mDataSource; 504d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks int sleepMs = 10; 505f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 506f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (dataSource != null) { 5070e0a636482418d6836bb783d2b00e9e3706dfc14Wei-Ta Chen while (!Thread.interrupted() && !mIsShutdown) { 50862dd11281ef6e12049dd6798b184157e9583d7d4Venkat String[] databaseUris = null; 50962dd11281ef6e12049dd6798b184157e9583d7d4Venkat boolean performRefresh = false; 51062dd11281ef6e12049dd6798b184157e9583d7d4Venkat synchronized (mRequestedRefresh) { 51162dd11281ef6e12049dd6798b184157e9583d7d4Venkat if (mRequestedRefresh.size() > 0) { 51262dd11281ef6e12049dd6798b184157e9583d7d4Venkat // We prune this first. 51362dd11281ef6e12049dd6798b184157e9583d7d4Venkat int numRequests = mRequestedRefresh.size(); 51462dd11281ef6e12049dd6798b184157e9583d7d4Venkat for (int i = 0; i < numRequests; ++i) { 51562dd11281ef6e12049dd6798b184157e9583d7d4Venkat databaseUris = ArrayUtils.addAll(databaseUris, mRequestedRefresh.get(i)); 51662dd11281ef6e12049dd6798b184157e9583d7d4Venkat } 51762dd11281ef6e12049dd6798b184157e9583d7d4Venkat mRequestedRefresh.clear(); 51862dd11281ef6e12049dd6798b184157e9583d7d4Venkat performRefresh = true; 51962dd11281ef6e12049dd6798b184157e9583d7d4Venkat // We need to eliminate duplicate uris in this array 52062dd11281ef6e12049dd6798b184157e9583d7d4Venkat final HashMap<String, String> uris = new HashMap<String, String>(); 52162dd11281ef6e12049dd6798b184157e9583d7d4Venkat if (databaseUris != null) { 52262dd11281ef6e12049dd6798b184157e9583d7d4Venkat int numUris = databaseUris.length; 52362dd11281ef6e12049dd6798b184157e9583d7d4Venkat for (int i = 0; i < numUris; ++i) { 52462dd11281ef6e12049dd6798b184157e9583d7d4Venkat final String uri = databaseUris[i]; 52562dd11281ef6e12049dd6798b184157e9583d7d4Venkat if (uri != null) 52662dd11281ef6e12049dd6798b184157e9583d7d4Venkat uris.put(uri, uri); 52762dd11281ef6e12049dd6798b184157e9583d7d4Venkat } 52862dd11281ef6e12049dd6798b184157e9583d7d4Venkat } 52962dd11281ef6e12049dd6798b184157e9583d7d4Venkat databaseUris = new String[0]; 53062dd11281ef6e12049dd6798b184157e9583d7d4Venkat databaseUris = (String[]) uris.keySet().toArray(databaseUris); 53162dd11281ef6e12049dd6798b184157e9583d7d4Venkat } 53262dd11281ef6e12049dd6798b184157e9583d7d4Venkat } 533b832709e16e0d018447f3d20755d3c22d09e2ba0Venkat boolean settingFeedAboutToChange = false; 53462dd11281ef6e12049dd6798b184157e9583d7d4Venkat if (performRefresh) { 5351c729acdafe147abda6b3479eafb17607f172780Venkat if (dataSource != null) { 536b832709e16e0d018447f3d20755d3c22d09e2ba0Venkat if (mListener != null) { 537b832709e16e0d018447f3d20755d3c22d09e2ba0Venkat settingFeedAboutToChange = true; 538b832709e16e0d018447f3d20755d3c22d09e2ba0Venkat mListener.onFeedAboutToChange(this); 539b832709e16e0d018447f3d20755d3c22d09e2ba0Venkat } 54062dd11281ef6e12049dd6798b184157e9583d7d4Venkat dataSource.refresh(this, databaseUris); 54171660902f0aa83113bf11a2933e92b08c524fed5Venkat mMediaFeedNeedsToRun = true; 5421c729acdafe147abda6b3479eafb17607f172780Venkat } 5431c729acdafe147abda6b3479eafb17607f172780Venkat } 5441c729acdafe147abda6b3479eafb17607f172780Venkat if (mListenerNeedsUpdate && !mMediaFeedNeedsToRun) { 545f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mListenerNeedsUpdate = false; 546f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mListener != null) 54746afd9b71156248a494ea618c37b5a373fd8e386Venkat synchronized (mMediaSets) { 54846afd9b71156248a494ea618c37b5a373fd8e386Venkat mListener.onFeedChanged(this, mListenerNeedsLayout); 54946afd9b71156248a494ea618c37b5a373fd8e386Venkat } 550f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks try { 551f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks Thread.sleep(sleepMs); 552f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } catch (InterruptedException e) { 553f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return; 554f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 555f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } else { 556f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks try { 557f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks Thread.sleep(sleepMs); 558f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } catch (InterruptedException e) { 559f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return; 560f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 561f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 562d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks sleepMs = 300; 563d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks if (!mMediaFeedNeedsToRun) 564d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks continue; 565d93f177bf2bc26f4ad64ca2f7725da8b0347a77fVenkat App app = App.get(mContext); 56662f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat if (app == null || app.isPaused()) 56762f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat continue; 568b832709e16e0d018447f3d20755d3c22d09e2ba0Venkat if (settingFeedAboutToChange) { 569b832709e16e0d018447f3d20755d3c22d09e2ba0Venkat updateListener(true); 570b832709e16e0d018447f3d20755d3c22d09e2ba0Venkat } 571d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mMediaFeedNeedsToRun = false; 572f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks ArrayList<MediaSet> mediaSets = mMediaSets; 573f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks synchronized (mediaSets) { 574f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int expandedSetIndex = mExpandedMediaSetIndex; 575f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (expandedSetIndex >= mMediaSets.size()) { 576f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks expandedSetIndex = Shared.INVALID; 577f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 578f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (expandedSetIndex == Shared.INVALID) { 579f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks // We purge the sets outside this visibleRange. 5802b04f591f758b7af03c43d90e91b939cbd515506Venkat Krishnaraj int numSets = mediaSets.size(); 581f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks IndexRange visibleRange = mVisibleRange; 582f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks IndexRange bufferedRange = mBufferedRange; 583f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks boolean scanMediaSets = true; 584f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks for (int i = 0; i < numSets; ++i) { 585f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (i >= visibleRange.begin && i <= visibleRange.end && scanMediaSets) { 586f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet set = mediaSets.get(i); 587f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int numItemsLoaded = set.mNumItemsLoaded; 588f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (numItemsLoaded < set.getNumExpectedItems() && numItemsLoaded < 8) { 58962f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat synchronized (set) { 59062f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat dataSource.loadItemsForSet(this, set, numItemsLoaded, 8); 591eb50ae54adfb694b1a424e7a36eed99dc0be1620Venkat set.checkForDeletedItems(); 59262f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat } 593f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (set.getNumExpectedItems() == 0) { 594f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mediaSets.remove(set); 595f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks break; 596f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 597f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mListener != null) { 5981c729acdafe147abda6b3479eafb17607f172780Venkat mListenerNeedsUpdate = false; 5991c729acdafe147abda6b3479eafb17607f172780Venkat mListener.onFeedChanged(this, mListenerNeedsLayout); 6001c729acdafe147abda6b3479eafb17607f172780Venkat mListenerNeedsLayout = false; 601f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 602f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks sleepMs = 100; 603f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks scanMediaSets = false; 604f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 605d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks if (!set.setContainsValidItems()) { 606d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mediaSets.remove(set); 607d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks if (mListener != null) { 6081c729acdafe147abda6b3479eafb17607f172780Venkat mListenerNeedsUpdate = false; 6091c729acdafe147abda6b3479eafb17607f172780Venkat mListener.onFeedChanged(this, mListenerNeedsLayout); 6101c729acdafe147abda6b3479eafb17607f172780Venkat mListenerNeedsLayout = false; 611d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks } 612d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks break; 613d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks } 614f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 615f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 6162b04f591f758b7af03c43d90e91b939cbd515506Venkat Krishnaraj numSets = mediaSets.size(); 617f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks for (int i = 0; i < numSets; ++i) { 618f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet set = mediaSets.get(i); 619f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (i >= bufferedRange.begin && i <= bufferedRange.end) { 620f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (scanMediaSets) { 621f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int numItemsLoaded = set.mNumItemsLoaded; 622f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (numItemsLoaded < set.getNumExpectedItems() && numItemsLoaded < 8) { 62362f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat synchronized (set) { 62462f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat dataSource.loadItemsForSet(this, set, numItemsLoaded, 8); 625eb50ae54adfb694b1a424e7a36eed99dc0be1620Venkat set.checkForDeletedItems(); 62662f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat } 627f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (set.getNumExpectedItems() == 0) { 628f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mediaSets.remove(set); 629f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks break; 630f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 631f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mListener != null) { 6321c729acdafe147abda6b3479eafb17607f172780Venkat mListenerNeedsUpdate = false; 6331c729acdafe147abda6b3479eafb17607f172780Venkat mListener.onFeedChanged(this, mListenerNeedsLayout); 6341c729acdafe147abda6b3479eafb17607f172780Venkat mListenerNeedsLayout = false; 635f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 636f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks sleepMs = 100; 637f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks scanMediaSets = false; 638f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 639f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 640897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj } else if (!mListenerNeedsUpdate && (i < bufferedRange.begin || i > bufferedRange.end)) { 641f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks // Purge this set to its initial status. 642f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaClustering clustering = mClusterSets.get(set); 643f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (clustering != null) { 644f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks clustering.clear(); 645f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mClusterSets.remove(set); 646f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 647f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (set.getNumItems() != 0) 648f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks set.clear(); 649f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 650f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 651f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 652f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (expandedSetIndex != Shared.INVALID) { 653f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int numSets = mMediaSets.size(); 654f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks for (int i = 0; i < numSets; ++i) { 655f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks // Purge other sets. 656f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (i != expandedSetIndex) { 657f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet set = mediaSets.get(i); 658f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaClustering clustering = mClusterSets.get(set); 659f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (clustering != null) { 660f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks clustering.clear(); 661f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mClusterSets.remove(set); 662f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 663897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj if (set.mNumItemsLoaded != 0) 664f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks set.clear(); 665f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 666f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 667f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks // Make sure all the items are loaded for the album. 668f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int numItemsLoaded = mediaSets.get(expandedSetIndex).mNumItemsLoaded; 669f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int requestedItems = mVisibleRange.end; 670f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks // requestedItems count changes in clustering mode. 671f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mInClusteringMode && mClusterSets != null) { 672f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks requestedItems = 0; 673f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaClustering clustering = mClusterSets.get(mediaSets.get(expandedSetIndex)); 674db87908d640e17ef0946824be158d86c6e2d6836Dave Sparks if (clustering != null) { 675db87908d640e17ef0946824be158d86c6e2d6836Dave Sparks ArrayList<Cluster> clusters = clustering.getClustersForDisplay(); 676db87908d640e17ef0946824be158d86c6e2d6836Dave Sparks int numClusters = clusters.size(); 677db87908d640e17ef0946824be158d86c6e2d6836Dave Sparks for (int i = 0; i < numClusters; i++) { 678db87908d640e17ef0946824be158d86c6e2d6836Dave Sparks requestedItems += clusters.get(i).getNumExpectedItems(); 679db87908d640e17ef0946824be158d86c6e2d6836Dave Sparks } 680f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 681f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 682f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet set = mediaSets.get(expandedSetIndex); 683f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (numItemsLoaded < set.getNumExpectedItems()) { 684897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj // We perform calculations for a window that gets 685897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj // anchored to a multiple of NUM_ITEMS_LOOKAHEAD. 686897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj // The start of the window is 0, x, 2x, 3x ... etc 687897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj // where x = NUM_ITEMS_LOOKAHEAD. 68862f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat synchronized (set) { 68962f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat dataSource.loadItemsForSet(this, set, numItemsLoaded, (requestedItems / NUM_ITEMS_LOOKAHEAD) 69062f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat * NUM_ITEMS_LOOKAHEAD + NUM_ITEMS_LOOKAHEAD); 691eb50ae54adfb694b1a424e7a36eed99dc0be1620Venkat set.checkForDeletedItems(); 69262f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat } 693f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (set.getNumExpectedItems() == 0) { 694f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mediaSets.remove(set); 6951c729acdafe147abda6b3479eafb17607f172780Venkat mListenerNeedsUpdate = false; 6961c729acdafe147abda6b3479eafb17607f172780Venkat mListener.onFeedChanged(this, mListenerNeedsLayout); 6971c729acdafe147abda6b3479eafb17607f172780Venkat mListenerNeedsLayout = false; 698f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 699f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (numItemsLoaded != set.mNumItemsLoaded && mListener != null) { 7001c729acdafe147abda6b3479eafb17607f172780Venkat mListenerNeedsUpdate = false; 7011c729acdafe147abda6b3479eafb17607f172780Venkat mListener.onFeedChanged(this, mListenerNeedsLayout); 7021c729acdafe147abda6b3479eafb17607f172780Venkat mListenerNeedsLayout = false; 703f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 704f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 705f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 706f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaFilter filter = mMediaFilter; 707f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (filter != null && mMediaFilteredSet == null) { 708f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (expandedSetIndex != Shared.INVALID) { 709f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet set = mediaSets.get(expandedSetIndex); 710f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks ArrayList<MediaItem> items = set.getItems(); 711f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int numItems = set.getNumItems(); 712f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet filteredSet = new MediaSet(); 713f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks filteredSet.setNumExpectedItems(numItems); 714f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mMediaFilteredSet = filteredSet; 715f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks for (int i = 0; i < numItems; ++i) { 716f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaItem item = items.get(i); 717f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (filter.pass(item)) { 718f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks filteredSet.addItem(item); 719f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 720f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 721f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks filteredSet.updateNumExpectedItems(); 722f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks filteredSet.generateTitle(true); 723f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 724f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks updateListener(true); 725f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 726f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 727f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 728f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 729f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 730f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 731f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void expandMediaSet(int mediaSetIndex) { 732f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks // We need to check if this slot can be focused or not. 733f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mListener != null) { 734f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mListener.onFeedAboutToChange(this); 735f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 736f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mExpandedMediaSetIndex > 0 && mediaSetIndex == Shared.INVALID) { 737f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks // We are collapsing a previously expanded media set 7381bb0c42b2a62f580eea4764d6a4434ffecfbf353Dave Sparks if (mediaSetIndex < mMediaSets.size() && mExpandedMediaSetIndex >= 0 && mExpandedMediaSetIndex < mMediaSets.size()) { 739f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet set = mMediaSets.get(mExpandedMediaSetIndex); 740f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (set.getNumItems() == 0) { 741f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks set.clear(); 742f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 743f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 744f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 745f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mExpandedMediaSetIndex = mediaSetIndex; 746f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mediaSetIndex < mMediaSets.size() && mediaSetIndex >= 0) { 747f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks // Notify Picasa that the user entered the album. 748f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks // MediaSet set = mMediaSets.get(mediaSetIndex); 7491bb0c42b2a62f580eea4764d6a4434ffecfbf353Dave Sparks // PicasaService.requestSync(mContext, 7501bb0c42b2a62f580eea4764d6a4434ffecfbf353Dave Sparks // PicasaService.TYPE_ALBUM_PHOTOS, set.mPicasaAlbumId); 751f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 752f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks updateListener(true); 753d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mMediaFeedNeedsToRun = true; 754f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 755f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 756f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public boolean canExpandSet(int slotIndex) { 757f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int mediaSetIndex = slotIndex; 758f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mediaSetIndex < mMediaSets.size() && mediaSetIndex >= 0) { 759f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet set = mMediaSets.get(mediaSetIndex); 760f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (set.getNumItems() > 0) { 761f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaItem item = set.getItems().get(0); 762f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (item.mId == Shared.INVALID) { 763f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return false; 764f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 765f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return true; 766f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 767f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 768f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return false; 769f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 770f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 771f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public boolean hasExpandedMediaSet() { 772f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return (mExpandedMediaSetIndex != Shared.INVALID); 773f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 774f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 775f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public boolean restorePreviousClusteringState() { 776f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks boolean retVal = disableClusteringIfNecessary(); 777f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (retVal) { 778f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mListener != null) { 779f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mListener.onFeedAboutToChange(this); 780f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 781f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks updateListener(true); 782d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mMediaFeedNeedsToRun = true; 783f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 784f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return retVal; 785f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 786f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 787f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks private boolean disableClusteringIfNecessary() { 788f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mInClusteringMode) { 789f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks // Disable clustering. 790f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mInClusteringMode = false; 791d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mMediaFeedNeedsToRun = true; 792f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return true; 793f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 794f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return false; 795f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 796f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 797f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public boolean isClustered() { 798f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return mInClusteringMode; 799f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 800f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 801f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public MediaSet getCurrentSet() { 802f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mExpandedMediaSetIndex != Shared.INVALID && mExpandedMediaSetIndex < mMediaSets.size()) { 803f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return mMediaSets.get(mExpandedMediaSetIndex); 804f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 805f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return null; 806f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 807f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 808f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void performClustering() { 809f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mListener != null) { 810f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mListener.onFeedAboutToChange(this); 811f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 812f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet setToUse = null; 8132a6f302015b435941369d94f04f3018972558c46Venkat Krishnaraj if (mExpandedMediaSetIndex != Shared.INVALID && mExpandedMediaSetIndex < mMediaSets.size()) { 814f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks setToUse = mMediaSets.get(mExpandedMediaSetIndex); 815f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 816f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (setToUse != null) { 817f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaClustering clustering = null; 818d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks synchronized (mClusterSets) { 819f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks // Make sure the computation is completed to the end. 820f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks clustering = mClusterSets.get(setToUse); 821f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (clustering != null) { 822f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks clustering.compute(null, true); 823f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } else { 824f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return; 825f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 826f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 827f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mInClusteringMode = true; 828f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks updateListener(true); 829f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 830f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 831f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 832f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void moveSetToFront(MediaSet mediaSet) { 833f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks ArrayList<MediaSet> mediaSets = mMediaSets; 834f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int numSets = mediaSets.size(); 835f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (numSets == 0) { 836f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mediaSets.add(mediaSet); 837f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return; 838f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 839f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet setToFind = mediaSets.get(0); 840f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (setToFind == mediaSet) { 841f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return; 842f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 843f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mediaSets.set(0, mediaSet); 844f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks int indexToSwapTill = -1; 845f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks for (int i = 1; i < numSets; ++i) { 846f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet set = mediaSets.get(i); 847f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (set == mediaSet) { 848f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mediaSets.set(i, setToFind); 849f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks indexToSwapTill = i; 850f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks break; 851f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 852f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 853f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (indexToSwapTill != Shared.INVALID) { 854f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks for (int i = indexToSwapTill; i > 1; --i) { 855f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet setEnd = mediaSets.get(i); 856f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks MediaSet setPrev = mediaSets.get(i - 1); 857f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mediaSets.set(i, setPrev); 858f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mediaSets.set(i - 1, setEnd); 859f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 860f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 861d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks mMediaFeedNeedsToRun = true; 862f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 863f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 864f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public MediaSet replaceMediaSet(long setId, DataSource dataSource) { 8651c729acdafe147abda6b3479eafb17607f172780Venkat Log.i(TAG, "Replacing media set " + setId); 8661b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj final MediaSet set = getMediaSet(setId); 867db6a1a6960d791d6004694da52b747de58886634Venkat if (set != null) 868db6a1a6960d791d6004694da52b747de58886634Venkat set.refresh(); 8691b329b810c628b43c86a574ec6d4ad4478f02d3cVenkat Krishnaraj return set; 870f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 871d8a6d17db8aaef2ea06c78fe643f06601d8aec5dDave Sparks 872f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public void setSingleImageMode(boolean singleImageMode) { 873f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks mSingleImageMode = singleImageMode; 874f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 875f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 876f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public boolean isSingleImageMode() { 877f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return mSingleImageMode; 878f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 879f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks 880f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks public MediaSet getExpandedMediaSet() { 881f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mExpandedMediaSetIndex == Shared.INVALID) 882f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return null; 883f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks if (mExpandedMediaSetIndex >= mMediaSets.size()) 884f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return null; 885f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks return mMediaSets.get(mExpandedMediaSetIndex); 886f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks } 8875e0474e8e1942c41234eb251f948c2fbb7445b02Venkat Krishnaraj 8885e0474e8e1942c41234eb251f948c2fbb7445b02Venkat Krishnaraj public void refresh() { 88962dd11281ef6e12049dd6798b184157e9583d7d4Venkat if (mDataSource != null) { 89062dd11281ef6e12049dd6798b184157e9583d7d4Venkat synchronized (mRequestedRefresh) { 89162dd11281ef6e12049dd6798b184157e9583d7d4Venkat mRequestedRefresh.add(mDataSource.getDatabaseUris()); 89262dd11281ef6e12049dd6798b184157e9583d7d4Venkat } 89362dd11281ef6e12049dd6798b184157e9583d7d4Venkat } 894897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj } 895897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj 896897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj private void refresh(final String[] databaseUris) { 89762f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat synchronized (mMediaSets) { 89862f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat if (mDataSource != null) { 89962dd11281ef6e12049dd6798b184157e9583d7d4Venkat synchronized (mRequestedRefresh) { 90062dd11281ef6e12049dd6798b184157e9583d7d4Venkat mRequestedRefresh.add(databaseUris); 90162dd11281ef6e12049dd6798b184157e9583d7d4Venkat } 90262f6a6a2e299c0706bfd0d4a0bca0ef1b10d3f37Venkat } 9035e0474e8e1942c41234eb251f948c2fbb7445b02Venkat Krishnaraj } 9045e0474e8e1942c41234eb251f948c2fbb7445b02Venkat Krishnaraj } 905897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj 906897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj public void onPause() { 907897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj final HashMap<String, ContentObserver> observers = mContentObservers; 9081b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat final int numObservers = observers.size(); 9091b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat if (numObservers > 0) { 9101b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat String[] uris = new String[numObservers]; 9111b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat final Set<String> keySet = observers.keySet(); 9121b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat if (keySet != null) { 9131b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat uris = keySet.toArray(uris); 9141b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat final int numUris = uris.length; 9151b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat final ContentResolver cr = mContext.getContentResolver(); 9161b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat for (int i = 0; i < numUris; ++i) { 9171b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat final String uri = uris[i]; 9181b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat if (uri != null) { 9191b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat final ContentObserver observer = observers.get(uri); 9201b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat cr.unregisterContentObserver(observer); 9211b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat observers.remove(uri); 9221b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat } 9231b2ccb78ad045ad14f4f18e218003bf317e7c85eVenkat } 924897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj } 925897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj } 926897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj observers.clear(); 927897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj } 928897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj 929897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj public void onResume() { 9305a626da1e8c21ee7c9e5f4d8142fc5adf7252f10Venkat final Context context = mContext; 9315a626da1e8c21ee7c9e5f4d8142fc5adf7252f10Venkat final DataSource dataSource = mDataSource; 9325a626da1e8c21ee7c9e5f4d8142fc5adf7252f10Venkat if (context == null || dataSource == null) 9335a626da1e8c21ee7c9e5f4d8142fc5adf7252f10Venkat return; 934897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj // We setup the listeners for this datasource 9355a626da1e8c21ee7c9e5f4d8142fc5adf7252f10Venkat final String[] uris = dataSource.getDatabaseUris(); 936897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj final HashMap<String, ContentObserver> observers = mContentObservers; 9375a626da1e8c21ee7c9e5f4d8142fc5adf7252f10Venkat if (context instanceof Gallery) { 9385a626da1e8c21ee7c9e5f4d8142fc5adf7252f10Venkat final Gallery gallery = (Gallery) context; 9395a626da1e8c21ee7c9e5f4d8142fc5adf7252f10Venkat final ContentResolver cr = context.getContentResolver(); 940897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj if (uris != null) { 941897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj final int numUris = uris.length; 942897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj for (int i = 0; i < numUris; ++i) { 943897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj final String uri = uris[i]; 944897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj final ContentObserver presentObserver = observers.get(uri); 945897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj if (presentObserver == null) { 9465a626da1e8c21ee7c9e5f4d8142fc5adf7252f10Venkat final Handler handler = App.get(context).getHandler(); 947897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj final ContentObserver observer = new ContentObserver(handler) { 948897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj public void onChange(boolean selfChange) { 949897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj if (!mWaitingForMediaScanner) { 950897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj MediaFeed.this.refresh(new String[] { uri }); 951897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj } 952897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj } 953897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj }; 95462dd11281ef6e12049dd6798b184157e9583d7d4Venkat cr.registerContentObserver(Uri.parse(uri), true, observer); 955897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj observers.put(uri, observer); 956897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj } 957897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj } 958897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj } 959897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj } 960897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj refresh(); 961897ad622181117e8acbdeae2ff0afece68f1332cVenkat Krishnaraj } 962f99dfe8549fb6c2c06c8cb7ca7d5eb33002c809eDave Sparks} 963