Fix 5835642: Only finish the page when it's still active.

The original code can call finishState twice in a row with the calling
stack: finishState -> onPause -> clearLoadingBit -> finishState.

Change clearLoadingBit to call finishState only when it's currently active.

Change-Id: I537dbdd6ce26b26aeb565e243c4168229d4a70ef
diff --git a/src/com/android/gallery3d/app/AlbumPage.java b/src/com/android/gallery3d/app/AlbumPage.java
index 6fb4143..fbe4533 100644
--- a/src/com/android/gallery3d/app/AlbumPage.java
+++ b/src/com/android/gallery3d/app/AlbumPage.java
@@ -349,8 +349,10 @@
         if (mSyncTask != null) {
             mSyncTask.cancel();
             mSyncTask = null;
+            clearLoadingBit(BIT_LOADING_SYNC);
         }
         mActionModeHandler.pause();
+        GalleryUtils.setSpinnerVisibility((Activity) mActivity, false);
     }
 
     @Override
@@ -572,9 +574,8 @@
                 if (resultCode == MediaSet.SYNC_RESULT_SUCCESS) {
                     mInitialSynced = true;
                 }
-                if (!mIsActive) return;
                 clearLoadingBit(BIT_LOADING_SYNC);
-                if (resultCode == MediaSet.SYNC_RESULT_ERROR) {
+                if (resultCode == MediaSet.SYNC_RESULT_ERROR && mIsActive) {
                     Toast.makeText((Context) mActivity, R.string.sync_album_error,
                             Toast.LENGTH_LONG).show();
                 }
@@ -583,7 +584,7 @@
     }
 
     private void setLoadingBit(int loadTaskBit) {
-        if (mLoadingBits == 0) {
+        if (mLoadingBits == 0 && mIsActive) {
             GalleryUtils.setSpinnerVisibility((Activity) mActivity, true);
         }
         mLoadingBits |= loadTaskBit;
@@ -591,7 +592,7 @@
 
     private void clearLoadingBit(int loadTaskBit) {
         mLoadingBits &= ~loadTaskBit;
-        if (mLoadingBits == 0) {
+        if (mLoadingBits == 0 && mIsActive) {
             GalleryUtils.setSpinnerVisibility((Activity) mActivity, false);
 
             if (mAlbumDataAdapter.size() == 0) {
@@ -610,7 +611,6 @@
 
         @Override
         public void onLoadingFinished() {
-            if (!mIsActive) return;
             clearLoadingBit(BIT_LOADING_RELOAD);
         }
     }
diff --git a/src/com/android/gallery3d/app/AlbumSetPage.java b/src/com/android/gallery3d/app/AlbumSetPage.java
index e1dcade..5c39341 100644
--- a/src/com/android/gallery3d/app/AlbumSetPage.java
+++ b/src/com/android/gallery3d/app/AlbumSetPage.java
@@ -292,7 +292,7 @@
 
     private void clearLoadingBit(int loadingBit) {
         mLoadingBits &= ~loadingBit;
-        if (mLoadingBits == 0) {
+        if (mLoadingBits == 0 && mIsActive) {
             GalleryUtils.setSpinnerVisibility((Activity) mActivity, false);
 
             // Only show toast when there's no album and we are going to finish
@@ -308,7 +308,7 @@
     }
 
     private void setLoadingBit(int loadingBit) {
-        if (mLoadingBits == 0) {
+        if (mLoadingBits == 0 && mIsActive) {
             GalleryUtils.setSpinnerVisibility((Activity) mActivity, true);
         }
         mLoadingBits |= loadingBit;
@@ -330,6 +330,7 @@
             mSyncTask = null;
             clearLoadingBit(BIT_LOADING_SYNC);
         }
+        GalleryUtils.setSpinnerVisibility((Activity) mActivity, false);
     }
 
     @Override
@@ -614,9 +615,8 @@
                 if (resultCode == MediaSet.SYNC_RESULT_SUCCESS) {
                     mInitialSynced = true;
                 }
-                if (!mIsActive) return;
                 clearLoadingBit(BIT_LOADING_SYNC);
-                if (resultCode == MediaSet.SYNC_RESULT_ERROR) {
+                if (resultCode == MediaSet.SYNC_RESULT_ERROR && mIsActive) {
                     Toast.makeText((Context) mActivity, R.string.sync_album_set_error,
                             Toast.LENGTH_LONG).show();
                 }
@@ -630,7 +630,6 @@
         }
 
         public void onLoadingFinished() {
-            if (!mIsActive) return;
             clearLoadingBit(BIT_LOADING_RELOAD);
         }
     }
diff --git a/src/com/android/gallery3d/app/StateManager.java b/src/com/android/gallery3d/app/StateManager.java
index 556a06a..f171689 100644
--- a/src/com/android/gallery3d/app/StateManager.java
+++ b/src/com/android/gallery3d/app/StateManager.java
@@ -159,7 +159,7 @@
     }
 
     void finishState(ActivityState state) {
-        Log.v(TAG, "finishState " + state.getClass());
+        Log.v(TAG, "finishState " + state);
         if (state != mStack.peek().activityState) {
             if (state.isDestroyed()) {
                 Log.d(TAG, "The state is already destroyed");