Merge "Recycle textures when we don't need them." into jb-dev
diff --git a/src/com/android/gallery3d/app/PhotoPage.java b/src/com/android/gallery3d/app/PhotoPage.java
index 71a6c8e..2132d06 100644
--- a/src/com/android/gallery3d/app/PhotoPage.java
+++ b/src/com/android/gallery3d/app/PhotoPage.java
@@ -75,6 +75,7 @@
     private static final int MSG_ON_FULL_SCREEN_CHANGED = 4;
     private static final int MSG_UPDATE_ACTION_BAR = 5;
     private static final int MSG_UNFREEZE_GLROOT = 6;
+    private static final int MSG_WANT_BARS = 7;
 
     private static final int HIDE_BARS_TIMEOUT = 3500;
     private static final int UNFREEZE_GLROOT_TIMEOUT = 250;
@@ -110,8 +111,6 @@
     private int mCurrentIndex = 0;
     private Handler mHandler;
     private boolean mShowBars = true;
-    // The value of canShowBars() last time the bar updates state.
-    private boolean mCanShowBars = false;
     private volatile boolean mActionBarAllowed = true;
     private GalleryActionBar mActionBar;
     private MyMenuVisibilityListener mMenuVisibilityListener;
@@ -269,12 +268,10 @@
                     }
                     case MSG_LOCK_ORIENTATION: {
                         mOrientationManager.lockOrientation();
-                        updateBars();
                         break;
                     }
                     case MSG_UNLOCK_ORIENTATION: {
                         mOrientationManager.unlockOrientation();
-                        updateBars();
                         break;
                     }
                     case MSG_ON_FULL_SCREEN_CHANGED: {
@@ -285,6 +282,10 @@
                         updateBars();
                         break;
                     }
+                    case MSG_WANT_BARS: {
+                        wantBars();
+                        break;
+                    }
                     case MSG_UNFREEZE_GLROOT: {
                         mActivity.getGLRoot().unfreeze();
                         break;
@@ -378,6 +379,7 @@
     private void showBars() {
         if (mShowBars) return;
         mShowBars = true;
+        mOrientationManager.unlockOrientation();
         mActionBar.show();
         mActivity.getGLRoot().setLightsOutMode(false);
         refreshHidingMessage();
@@ -403,29 +405,24 @@
         if (mAppBridge != null && mCurrentIndex == 0) return false;
         // No bars if it's not allowed.
         if (!mActionBarAllowed) return false;
-        // No bars if the orientation is locked.
-        if (mOrientationManager.isOrientationLocked()) return false;
 
         return true;
     }
 
+    private void wantBars() {
+        if (canShowBars()) showBars();
+    }
+
     private void toggleBars() {
-        mCanShowBars = canShowBars();
         if (mShowBars) {
             hideBars();
         } else {
-            if (mCanShowBars) showBars();
+            if (canShowBars()) showBars();
         }
     }
 
     private void updateBars() {
-        boolean v = canShowBars();
-        if (mCanShowBars == v) return;
-        mCanShowBars = v;
-
-        if (mCanShowBars) {
-            showBars();
-        } else {
+        if (!canShowBars()) {
             hideBars();
         }
     }
@@ -687,6 +684,11 @@
     }
 
     @Override
+    public void onActionBarWanted() {
+        mHandler.sendEmptyMessage(MSG_WANT_BARS);
+    }
+
+    @Override
     public void onFullScreenChanged(boolean full) {
         Message m = mHandler.obtainMessage(
                 MSG_ON_FULL_SCREEN_CHANGED, full ? 1 : 0, 0);
diff --git a/src/com/android/gallery3d/ui/PhotoView.java b/src/com/android/gallery3d/ui/PhotoView.java
index a15dd61..4490569 100644
--- a/src/com/android/gallery3d/ui/PhotoView.java
+++ b/src/com/android/gallery3d/ui/PhotoView.java
@@ -90,33 +90,25 @@
         public void unlockOrientation();
         public void onFullScreenChanged(boolean full);
         public void onActionBarAllowed(boolean allowed);
+        public void onActionBarWanted();
         public void onCurrentImageUpdated();
     }
 
-    // Here is a graph showing the places we need to lock/unlock device
-    // orientation:
+    // The rules about orientation locking:
     //
-    //           +------------+ A  +------------+
-    // Page mode |   Camera   |<---|   Photo    |
-    //           |  [locked]  |--->| [unlocked] |
-    //           +------------+  B +------------+
-    //                ^                  ^
-    //                | C                | D
-    //           +------------+    +------------+
-    //           |   Camera   |    |   Photo    |
-    // Film mode |    [*]     |    |    [*]     |
-    //           +------------+    +------------+
+    // (1) We need to lock the orientation if we are in page mode camera
+    // preview, so there is no (unwanted) rotation animation when the user
+    // rotates the device.
     //
-    // In Page mode, we want to lock in Camera because we don't want the system
-    // rotation animation. We also want to unlock in Photo because we want to
-    // show the system action bar in the right place.
+    // (2) We need to unlock the orientation if we want to show the action bar
+    // because the action bar follows the system orientation.
     //
-    // We don't show action bar in Film mode, so it's fine for it to be locked
-    // or unlocked in Film mode.
+    // The rules about action bar:
     //
-    // There are four transitions we need to check if we need to
-    // lock/unlock. Marked as A to D above and in the code.
-
+    // (1) If we are in film mode, we don't show action bar.
+    //
+    // (2) If we go from camera to gallery with capture animation, we show
+    // action bar.
     private static final int MSG_CANCEL_EXTRA_SCALING = 2;
     private static final int MSG_SWITCH_FOCUS = 3;
     private static final int MSG_CAPTURE_ANIMATION_DONE = 4;
@@ -457,14 +449,9 @@
                 setFilmMode(false);
             }
 
-            if (isCenter && !mFilmMode) {
-                if (mIsCamera) {
-                    // move into camera, lock
-                    mListener.lockOrientation();  // Transition A
-                } else {
-                    // move out of camera, unlock
-                    mListener.unlockOrientation();  // Transition B
-                }
+            if (isCenter && !mFilmMode && mIsCamera) {
+                // Move into camera in page mode, lock
+                mListener.lockOrientation();
             }
 
             mWasCameraCenter = isCameraCenter;
@@ -931,13 +918,9 @@
         mModel.setNeedFullImage(!enabled);
         mListener.onActionBarAllowed(!enabled);
 
-        // If we leave filmstrip mode, we should lock/unlock
-        if (!enabled) {
-            if (mPictures.get(0).isCamera()) {
-                mListener.lockOrientation();  // Transition C
-            } else {
-                mListener.unlockOrientation();  // Transition D
-            }
+        // Move into camera in page mode, lock
+        if (!enabled && mPictures.get(0).isCamera()) {
+            mListener.lockOrientation();
         }
     }
 
@@ -1209,12 +1192,10 @@
 
     private void captureAnimationDone(int offset) {
         mHolding &= ~HOLD_CAPTURE_ANIMATION;
-        if (offset == 1) {
-            // move out of camera, unlock
-            if (!mFilmMode) {
-                // Now the capture animation is done, enable the action bar.
-                mListener.onActionBarAllowed(true);
-            }
+        if (offset == 1 && !mFilmMode) {
+            // Now the capture animation is done, enable the action bar.
+            mListener.onActionBarAllowed(true);
+            mListener.onActionBarWanted();
         }
         snapback();
     }
diff --git a/src/com/android/gallery3d/ui/PositionController.java b/src/com/android/gallery3d/ui/PositionController.java
index bb2b83e..d085c4d 100644
--- a/src/com/android/gallery3d/ui/PositionController.java
+++ b/src/com/android/gallery3d/ui/PositionController.java
@@ -209,8 +209,13 @@
         }
 
         updateScaleAndGapLimit();
-        startOpeningAnimationIfNeeded();
-        snapAndRedraw();
+
+        // If we have the opening animation, do it. Otherwise go directly to the
+        // right position.
+        if (!startOpeningAnimationIfNeeded()) {
+            snapAndRedraw();
+            skipAnimation();
+        }
     }
 
     public void setConstrainedFrame(Rect f) {
@@ -280,10 +285,10 @@
         return true;
     }
 
-    private void startOpeningAnimationIfNeeded() {
-        if (mOpenAnimationRect == null) return;
+    private boolean startOpeningAnimationIfNeeded() {
+        if (mOpenAnimationRect == null) return false;
         Box b = mBoxes.get(0);
-        if (b.mUseViewSize) return;
+        if (b.mUseViewSize) return false;
 
         // Start animation from the saved rectangle if we have one.
         Rect r = mOpenAnimationRect;
@@ -303,6 +308,8 @@
             g.mCurrentGap = mViewW;
             g.doAnimation(g.mDefaultSize, ANIM_KIND_OPENING);
         }
+
+        return true;
     }
 
     public void setFilmMode(boolean enabled) {