Fix 6259517 "0 images / videos available" toast is displayed in gallery on launch though it has images / videos

And removed sync error toast.

Bug: 6259517

Change-Id: Ib7b25176a6db290220661a2e98e4e031043201d9
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 1772f08..529480c 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -211,8 +211,6 @@
 
     <!-- This toast message is shown when failed to load the album data. [CHAR LIMIT=NONE] -->
     <string name="sync_album_error">Couldn\'t download the photos in this album. Retry later.</string>
-    <!-- This toast message is shown when failed to load the album list data. [CHAR LIMIT=NONE] -->
-    <string name="sync_album_set_error">Couldn\'t download the list of albums. Retry later.</string>
 
     <!-- The title of the menu item to let user choose the which portion of
          the media items the user wants to see. When pressed, a submenu will
diff --git a/src/com/android/gallery3d/app/AlbumPage.java b/src/com/android/gallery3d/app/AlbumPage.java
index 090ba08..e773691 100644
--- a/src/com/android/gallery3d/app/AlbumPage.java
+++ b/src/com/android/gallery3d/app/AlbumPage.java
@@ -652,7 +652,9 @@
                         mInitialSynced = true;
                     }
                     clearLoadingBit(BIT_LOADING_SYNC);
-                    if (resultCode == MediaSet.SYNC_RESULT_ERROR && mIsActive) {
+                    if (resultCode == MediaSet.SYNC_RESULT_ERROR && mIsActive
+                            && (mAlbumDataAdapter.size() == 0)) {
+                        // show error toast only if the album is empty
                         Toast.makeText((Context) mActivity, R.string.sync_album_error,
                                 Toast.LENGTH_LONG).show();
                     }
diff --git a/src/com/android/gallery3d/app/AlbumSetDataLoader.java b/src/com/android/gallery3d/app/AlbumSetDataLoader.java
index 819adcc..39d4a8b 100644
--- a/src/com/android/gallery3d/app/AlbumSetDataLoader.java
+++ b/src/com/android/gallery3d/app/AlbumSetDataLoader.java
@@ -329,7 +329,7 @@
             while (mActive) {
                 synchronized (this) {
                     if (mActive && !mDirty && updateComplete) {
-                        updateLoading(false);
+                        if (!mSource.isLoading()) updateLoading(false);
                         Utils.waitWithoutInterrupt(this);
                         continue;
                     }
diff --git a/src/com/android/gallery3d/app/AlbumSetPage.java b/src/com/android/gallery3d/app/AlbumSetPage.java
index 8376b27..aa72eb8 100644
--- a/src/com/android/gallery3d/app/AlbumSetPage.java
+++ b/src/com/android/gallery3d/app/AlbumSetPage.java
@@ -300,9 +300,9 @@
             // Only show toast when there's no album and we are going to finish
             // the page. Toast is redundant if we are going to stay on this page.
             if ((mAlbumSetDataAdapter.size() == 0)) {
-                Toast.makeText((Context) mActivity,
-                        R.string.empty_album, Toast.LENGTH_LONG).show();
                 if (mActivity.getStateManager().getStateCount() > 1) {
+                    Toast.makeText((Context) mActivity,
+                            R.string.empty_album, Toast.LENGTH_LONG).show();
                     mActivity.getStateManager().finishState(this);
                 }
             }
@@ -597,8 +597,7 @@
                     }
                     clearLoadingBit(BIT_LOADING_SYNC);
                     if (resultCode == MediaSet.SYNC_RESULT_ERROR && mIsActive) {
-                        Toast.makeText((Context) mActivity, R.string.sync_album_set_error,
-                                Toast.LENGTH_LONG).show();
+                        Log.w(TAG, "failed to load album set");
                     }
                 } finally {
                     root.unlockRenderThread();
diff --git a/src/com/android/gallery3d/data/ComboAlbumSet.java b/src/com/android/gallery3d/data/ComboAlbumSet.java
index 916b163..6d9a2a3 100644
--- a/src/com/android/gallery3d/data/ComboAlbumSet.java
+++ b/src/com/android/gallery3d/data/ComboAlbumSet.java
@@ -65,6 +65,14 @@
     }
 
     @Override
+    public boolean isLoading() {
+        for (int i = 0, n = mSets.length; i < n; ++i) {
+            if (mSets[i].isLoading()) return true;
+        }
+        return false;
+    }
+
+    @Override
     public long reload() {
         boolean changed = false;
         for (int i = 0, n = mSets.length; i < n; ++i) {
diff --git a/src/com/android/gallery3d/data/LocalAlbumSet.java b/src/com/android/gallery3d/data/LocalAlbumSet.java
index dbb5189..07741ef 100644
--- a/src/com/android/gallery3d/data/LocalAlbumSet.java
+++ b/src/com/android/gallery3d/data/LocalAlbumSet.java
@@ -100,6 +100,7 @@
     private final ChangeNotifier mNotifierVideo;
     private final String mName;
     private final Handler mHandler;
+    private boolean mIsLoading;
 
     private Future<ArrayList<MediaSet>> mLoadTask;
     private ArrayList<MediaSet> mLoadBuffer;
@@ -261,6 +262,11 @@
     }
 
     @Override
+    public synchronized boolean isLoading() {
+        return mIsLoading;
+    }
+
+    @Override
     // synchronized on this function for
     //   1. Prevent calling reload() concurrently.
     //   2. Prevent calling onFutureDone() and reload() concurrently
@@ -268,6 +274,7 @@
         // "|" is used instead of "||" because we want to clear both flags.
         if (mNotifierImage.isDirty() | mNotifierVideo.isDirty()) {
             if (mLoadTask != null) mLoadTask.cancel();
+            mIsLoading = true;
             mLoadTask = mApplication.getThreadPool().submit(new AlbumsLoader(), this);
         }
         if (mLoadBuffer != null) {
@@ -285,6 +292,7 @@
     public synchronized void onFutureDone(Future<ArrayList<MediaSet>> future) {
         if (mLoadTask != future) return; // ignore, wait for the latest task
         mLoadBuffer = future.get();
+        mIsLoading = false;
         if (mLoadBuffer == null) mLoadBuffer = new ArrayList<MediaSet>();
         mHandler.post(new Runnable() {
             @Override
diff --git a/src/com/android/gallery3d/data/MediaSet.java b/src/com/android/gallery3d/data/MediaSet.java
index ff9b8c3..67525b1 100644
--- a/src/com/android/gallery3d/data/MediaSet.java
+++ b/src/com/android/gallery3d/data/MediaSet.java
@@ -94,6 +94,14 @@
         return false;
     }
 
+    /**
+     * Method {@link #reload()} may process the loading task in background, this method tells
+     * its client whether the loading is still in process or not.
+     */
+    public boolean isLoading() {
+        return false;
+    }
+
     public int getTotalMediaItemCount() {
         int total = getMediaItemCount();
         for (int i = 0, n = getSubMediaSetCount(); i < n; i++) {
diff --git a/src/com/android/gallery3d/data/MtpDeviceSet.java b/src/com/android/gallery3d/data/MtpDeviceSet.java
index 6dcb0d2..1f26511 100644
--- a/src/com/android/gallery3d/data/MtpDeviceSet.java
+++ b/src/com/android/gallery3d/data/MtpDeviceSet.java
@@ -47,6 +47,7 @@
     private Future<ArrayList<MediaSet>> mLoadTask;
     private ArrayList<MediaSet> mDeviceSet = new ArrayList<MediaSet>();
     private ArrayList<MediaSet> mLoadBuffer;
+    private boolean mIsLoading;
 
     public MtpDeviceSet(Path path, GalleryApp application, MtpContext mtpContext) {
         super(path, nextVersionNumber());
@@ -113,9 +114,15 @@
     }
 
     @Override
+    public synchronized boolean isLoading() {
+        return mIsLoading;
+    }
+
+    @Override
     public synchronized long reload() {
         if (mNotifier.isDirty()) {
             if (mLoadTask != null) mLoadTask.cancel();
+            mIsLoading = true;
             mLoadTask = mApplication.getThreadPool().submit(new DevicesLoader(), this);
         }
         if (mLoadBuffer != null) {
@@ -133,6 +140,7 @@
     public synchronized void onFutureDone(Future<ArrayList<MediaSet>> future) {
         if (future != mLoadTask) return;
         mLoadBuffer = future.get();
+        mIsLoading = false;
         if (mLoadBuffer == null) mLoadBuffer = new ArrayList<MediaSet>();
 
         mHandler.post(new Runnable() {