Merge "Improve the power off fade animation." into jb-mr1-dev
diff --git a/services/java/com/android/server/power/DisplayPowerController.java b/services/java/com/android/server/power/DisplayPowerController.java
index 25d2944..82c3617 100644
--- a/services/java/com/android/server/power/DisplayPowerController.java
+++ b/services/java/com/android/server/power/DisplayPowerController.java
@@ -206,11 +206,9 @@
     // May be 0 if no warm-up is required.
     private int mLightSensorWarmUpTimeConfig;
 
-    // True if we should animate the backlight when turning the screen on or off, which
-    // tends to be efficient for LCD displays but not for OLED displays.
-    // False if we should play the electron beam animation instead, which is better for
-    // OLED displays.
-    private boolean mElectronBeamAnimatesBacklightConfig;
+    // True if we should fade the screen while turning it off, false if we should play
+    // a stylish electron beam animation instead.
+    private boolean mElectronBeamFadesConfig;
 
     // The pending power request.
     // Initially null until the first call to requestPowerState.
@@ -396,7 +394,7 @@
         mScreenBrightnessRangeMinimum = clampAbsoluteBrightness(screenBrightnessMinimum);
         mScreenBrightnessRangeMaximum = PowerManager.BRIGHTNESS_ON;
 
-        mElectronBeamAnimatesBacklightConfig = resources.getBoolean(
+        mElectronBeamFadesConfig = resources.getBoolean(
                 com.android.internal.R.bool.config_animateScreenLights);
 
         if (!DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT) {
@@ -682,8 +680,8 @@
                                 if (mPowerState.getElectronBeamLevel() == 1.0f) {
                                     mPowerState.dismissElectronBeam();
                                 } else if (mPowerState.prepareElectronBeam(
-                                        mElectronBeamAnimatesBacklightConfig ?
-                                                ElectronBeam.MODE_BLANK :
+                                        mElectronBeamFadesConfig ?
+                                                ElectronBeam.MODE_FADE :
                                                         ElectronBeam.MODE_WARM_UP)) {
                                     mElectronBeamOnAnimator.start();
                                 } else {
@@ -704,8 +702,8 @@
                         if (mPowerState.getElectronBeamLevel() == 0.0f) {
                             setScreenOn(false);
                         } else if (mPowerState.prepareElectronBeam(
-                                mElectronBeamAnimatesBacklightConfig ?
-                                        ElectronBeam.MODE_BLANK :
+                                mElectronBeamFadesConfig ?
+                                        ElectronBeam.MODE_FADE :
                                                 ElectronBeam.MODE_COOL_DOWN)
                                 && mPowerState.isScreenOn()) {
                             mElectronBeamOffAnimator.start();
diff --git a/services/java/com/android/server/power/ElectronBeam.java b/services/java/com/android/server/power/ElectronBeam.java
index 8c242f7..6a567ba 100644
--- a/services/java/com/android/server/power/ElectronBeam.java
+++ b/services/java/com/android/server/power/ElectronBeam.java
@@ -80,6 +80,7 @@
     private EGLContext mEglContext;
     private EGLSurface mEglSurface;
     private boolean mSurfaceVisible;
+    private float mSurfaceAlpha;
 
     // Texture names.  We only use one texture, which contains the screenshot.
     private final int[] mTexNames = new int[1];
@@ -90,9 +91,20 @@
     private final FloatBuffer mVertexBuffer = createNativeFloatBuffer(8);
     private final FloatBuffer mTexCoordBuffer = createNativeFloatBuffer(8);
 
+    /**
+     * Animates an electron beam warming up.
+     */
     public static final int MODE_WARM_UP = 0;
+
+    /**
+     * Animates an electron beam shutting off.
+     */
     public static final int MODE_COOL_DOWN = 1;
-    public static final int MODE_BLANK = 2;
+
+    /**
+     * Animates a simple dim layer to fade the contents of the screen in or out progressively.
+     */
+    public static final int MODE_FADE = 2;
 
     public ElectronBeam(Display display) {
         mDisplay = display;
@@ -138,7 +150,7 @@
 
     private boolean tryPrepare() {
         if (createSurface()) {
-            if (mMode == MODE_BLANK) {
+            if (mMode == MODE_FADE) {
                 return true;
             }
             return createEglContext()
@@ -182,7 +194,7 @@
             return false;
         }
 
-        if (mMode == MODE_BLANK) {
+        if (mMode == MODE_FADE) {
             return showSurface(1.0f - level);
         }
 
@@ -504,7 +516,7 @@
             if (mSurface == null) {
                 try {
                     int flags;
-                    if (mMode == MODE_BLANK) {
+                    if (mMode == MODE_FADE) {
                         flags = Surface.FX_SURFACE_DIM | Surface.HIDDEN;
                     } else {
                         flags = Surface.OPAQUE | Surface.HIDDEN;
@@ -579,11 +591,12 @@
             }
             mSurface = null;
             mSurfaceVisible = false;
+            mSurfaceAlpha = 0f;
         }
     }
 
     private boolean showSurface(float alpha) {
-        if (!mSurfaceVisible) {
+        if (!mSurfaceVisible || mSurfaceAlpha != alpha) {
             Surface.openTransaction();
             try {
                 mSurface.setLayer(ELECTRON_BEAM_LAYER);
@@ -593,6 +606,7 @@
                 Surface.closeTransaction();
             }
             mSurfaceVisible = true;
+            mSurfaceAlpha = alpha;
         }
         return true;
     }
@@ -683,5 +697,6 @@
         pw.println("  mDisplayWidth=" + mDisplayWidth);
         pw.println("  mDisplayHeight=" + mDisplayHeight);
         pw.println("  mSurfaceVisible=" + mSurfaceVisible);
+        pw.println("  mSurfaceAlpha=" + mSurfaceAlpha);
     }
 }