OSDN Git Service

Fix NPE in onFullScreenChanged message handler.
authorChih-Chung Chang <chihchung@google.com>
Thu, 3 May 2012 03:41:05 +0000 (11:41 +0800)
committerChih-Chung Chang <chihchung@google.com>
Thu, 3 May 2012 10:28:03 +0000 (18:28 +0800)
Bug: 6430929

Also tweak the timing of showing action bar and the timing of unlock orientation.

Change-Id: I1af4703ccf99f5257e9724a20d0aa216d20e13c2

src/com/android/gallery3d/app/OrientationManager.java
src/com/android/gallery3d/app/PhotoPage.java
src/com/android/gallery3d/ui/PhotoView.java

index 93a1fc5..a54f668 100644 (file)
@@ -109,6 +109,10 @@ public class OrientationManager {
         disableCompensation();
     }
 
+    public boolean isOrientationLocked() {
+        return mOrientationLocked;
+    }
+
     // Calculate the compensation value and send it to listeners.
     private void updateCompensation() {
         if (mOrientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
index 76d4f91..51a57f2 100644 (file)
@@ -100,6 +100,8 @@ public class PhotoPage extends ActivityState implements
     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;
@@ -260,10 +262,12 @@ public class PhotoPage extends ActivityState implements
                     }
                     case MSG_LOCK_ORIENTATION: {
                         mOrientationManager.lockOrientation();
+                        updateBars();
                         break;
                     }
                     case MSG_UNLOCK_ORIENTATION: {
                         mOrientationManager.unlockOrientation();
+                        updateBars();
                         break;
                     }
                     case MSG_ON_FULL_SCREEN_CHANGED: {
@@ -380,7 +384,7 @@ public class PhotoPage extends ActivityState implements
         mActionBar.hide();
         WindowManager.LayoutParams params =
                 ((Activity) mActivity).getWindow().getAttributes();
-        params.systemUiVisibility = View. SYSTEM_UI_FLAG_LOW_PROFILE;
+        params.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE;
         ((Activity) mActivity).getWindow().setAttributes(params);
         mHandler.removeMessages(MSG_HIDE_BARS);
     }
@@ -392,27 +396,38 @@ public class PhotoPage extends ActivityState implements
         }
     }
 
+    private boolean canShowBars() {
+        // No bars if we are showing camera preview.
+        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 toggleBars() {
+        mCanShowBars = canShowBars();
         if (mShowBars) {
             hideBars();
-        } else if (canShowBars()) {
-            showBars();
+        } else {
+            if (mCanShowBars) showBars();
         }
     }
 
     private void updateBars() {
-        if (canShowBars()) {
+        boolean v = canShowBars();
+        if (mCanShowBars == v) return;
+        mCanShowBars = v;
+
+        if (mCanShowBars) {
             showBars();
         } else {
             hideBars();
         }
     }
 
-    private boolean canShowBars() {
-        boolean atCamera = mAppBridge != null && mCurrentIndex == 0;
-        return mActionBarAllowed && !atCamera;
-    }
-
     @Override
     public void onOrientationCompensationChanged(int degrees) {
         mActivity.getGLRoot().setOrientationCompensation(degrees);
@@ -730,6 +745,9 @@ public class PhotoPage extends ActivityState implements
             mScreenNail = null;
         }
         mOrientationManager.removeListener(this);
+
+        // Remove all pending messages.
+        mHandler.removeCallbacksAndMessages(null);
         super.onDestroy();
     }
 
index be3d81f..adbf791 100644 (file)
@@ -473,19 +473,20 @@ public class PhotoView extends GLView {
             boolean isCenter = mPositionController.isCenter();
 
             if (mLoadingState == LOADING_COMPLETE) {
-                if (mIsCamera) {
-                    boolean full = !mFilmMode && isCenter
-                        && mPositionController.isAtMinimalScale();
-                    if (full != mFullScreen) {
-                        mFullScreen = full;
-                        mListener.onFullScreenChanged(full);
-                    }
-                }
                 setTileViewPosition(r);
                 PhotoView.super.render(canvas);
             }
             renderMessage(canvas, r.centerX(), r.centerY());
 
+            if (mIsCamera) {
+                boolean full = !mFilmMode && isCenter
+                        && mPositionController.isAtMinimalScale();
+                if (full != mFullScreen) {
+                    mFullScreen = full;
+                    mListener.onFullScreenChanged(full);
+                }
+            }
+
             // We want to have the following transitions:
             // (1) Move camera preview out of its place: switch to film mode
             // (2) Move camera preview into its place: switch to page mode
@@ -502,9 +503,16 @@ public class PhotoView extends GLView {
                 // setFilmMode(true);
             } else if (!mWasCameraCenter && isCameraCenter && mFilmMode) {
                 setFilmMode(false);
-            } else if (isCameraCenter && !mFilmMode) {
-                // move into camera, lock
-                mListener.lockOrientation();  // Transition A
+            }
+
+            if (isCenter && !mFilmMode) {
+                if (mIsCamera) {
+                    // move into camera, lock
+                    mListener.lockOrientation();  // Transition A
+                } else {
+                    // move out of camera, unlock
+                    mListener.unlockOrientation();  // Transition B
+                }
             }
 
             mWasCameraCenter = isCameraCenter;
@@ -1152,7 +1160,6 @@ public class PhotoView extends GLView {
         if (offset == 1) {
             // move out of camera, unlock
             if (!mFilmMode) {
-                mListener.unlockOrientation();  // Transition B
                 // Now the capture animation is done, enable the action bar.
                 mListener.onActionBarAllowed(true);
             }