Fix issue #3330037: Unnecessary orientations appear...

...when the device's physical orientation is portrait.

We now hold off on computing app token orientation while preparing
to open or close app tokens.

Also clean up a few other little issues.

Change-Id: Iae125a975c7706fb4d068c872fd172e69854ff15
diff --git a/services/java/com/android/server/ScreenRotationAnimation.java b/services/java/com/android/server/ScreenRotationAnimation.java
index 19cc203..2ad9cbe 100644
--- a/services/java/com/android/server/ScreenRotationAnimation.java
+++ b/services/java/com/android/server/ScreenRotationAnimation.java
@@ -60,7 +60,8 @@
     final Matrix mSnapshotFinalMatrix = new Matrix();
     final float[] mTmpFloats = new float[9];
 
-    public ScreenRotationAnimation(Context context, Display display, SurfaceSession session) {
+    public ScreenRotationAnimation(Context context, Display display, SurfaceSession session,
+            boolean inTransaction) {
         mContext = context;
         mDisplay = display;
 
@@ -84,60 +85,70 @@
         mOriginalWidth = mDisplayMetrics.widthPixels;
         mOriginalHeight = mDisplayMetrics.heightPixels;
 
-        Surface.openTransaction();
+        if (!inTransaction) {
+            if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG,
+                    ">>> OPEN TRANSACTION ScreenRotationAnimation");
+            Surface.openTransaction();
+        }
         
         try {
-            mSurface = new Surface(session, 0, "FreezeSurface",
-                    -1, mWidth, mHeight, PixelFormat.OPAQUE, 0);
-            mSurface.setLayer(WindowManagerService.TYPE_LAYER_MULTIPLIER * 200);
-        } catch (Surface.OutOfResourcesException e) {
-            Slog.w(TAG, "Unable to allocate freeze surface", e);
-        }
-        
-        if (false) {
             try {
-                int size = mOriginalWidth > mOriginalHeight ? mOriginalWidth : mOriginalHeight;
-                mBlackSurface = new Surface(session, 0, "BlackSurface",
-                        -1, size, size, PixelFormat.OPAQUE, Surface.FX_SURFACE_DIM);
-                mBlackSurface.setAlpha(1.0f);
-                mBlackSurface.setLayer(0);
+                mSurface = new Surface(session, 0, "FreezeSurface",
+                        -1, mWidth, mHeight, PixelFormat.OPAQUE, 0);
+                mSurface.setLayer(WindowManagerService.TYPE_LAYER_MULTIPLIER * 200);
             } catch (Surface.OutOfResourcesException e) {
-                Slog.w(TAG, "Unable to allocate black surface", e);
+                Slog.w(TAG, "Unable to allocate freeze surface", e);
             }
-        }
-        
-        setRotation(display.getRotation());
 
-        if (mSurface != null) {
-            Rect dirty = new Rect(0, 0, mWidth, mHeight);
-            Canvas c = null;
-            try {
-                c = mSurface.lockCanvas(dirty);
-            } catch (IllegalArgumentException e) {
-                Slog.w(TAG, "Unable to lock surface", e);
-                return;
-            } catch (Surface.OutOfResourcesException e) {
-                Slog.w(TAG, "Unable to lock surface", e);
-                return;
+            if (false) {
+                try {
+                    int size = mOriginalWidth > mOriginalHeight ? mOriginalWidth : mOriginalHeight;
+                    mBlackSurface = new Surface(session, 0, "BlackSurface",
+                            -1, size, size, PixelFormat.OPAQUE, Surface.FX_SURFACE_DIM);
+                    mBlackSurface.setAlpha(1.0f);
+                    mBlackSurface.setLayer(0);
+                } catch (Surface.OutOfResourcesException e) {
+                    Slog.w(TAG, "Unable to allocate black surface", e);
+                }
             }
-            if (c == null) {
-                Slog.w(TAG, "Null surface");
-                return;
+
+            setRotation(display.getRotation());
+
+            if (mSurface != null) {
+                Rect dirty = new Rect(0, 0, mWidth, mHeight);
+                Canvas c = null;
+                try {
+                    c = mSurface.lockCanvas(dirty);
+                } catch (IllegalArgumentException e) {
+                    Slog.w(TAG, "Unable to lock surface", e);
+                    return;
+                } catch (Surface.OutOfResourcesException e) {
+                    Slog.w(TAG, "Unable to lock surface", e);
+                    return;
+                }
+                if (c == null) {
+                    Slog.w(TAG, "Null surface");
+                    return;
+                }
+        
+                if (screenshot != null) {
+                    c.drawBitmap(screenshot, 0, 0, new Paint(0));
+                } else {
+                    c.drawColor(Color.GREEN);
+                }
+
+                mSurface.unlockCanvasAndPost(c);
+            }
+        } finally {
+            if (!inTransaction) {
+                Surface.closeTransaction();
+                if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG,
+                        "<<< CLOSE TRANSACTION ScreenRotationAnimation");
             }
     
             if (screenshot != null) {
-                c.drawBitmap(screenshot, 0, 0, new Paint(0));
-            } else {
-                c.drawColor(Color.GREEN);
+                screenshot.recycle();
             }
-    
-            mSurface.unlockCanvasAndPost(c);
-        }
-        
-        Surface.closeTransaction();
-
-        if (screenshot != null) {
-            screenshot.recycle();
         }
     }