Only supports ambient mode when necessary.

We only need support ambient mode on specified devices but we currently
enable this attribute on all devices, that means we need redraw every
time when ambient mode changes. However, we can just show / hide
wallpaper window on the devices which not support ambient transition.

In addition, always use portrait dimension as scissor area since we only
do transition in portrait.

Bug: 138021396
Bug: 137962047
Test: Manually test on F2, C1, B4, S4 and walleye.
Test: Set an image wallpaper, switch between home, aod and lockscreen.
Test: Also use finger print to unlock from aod to home directly.
Test: Rotate to landscape, set a new image wallpaper.
Test: Switch between home, aod and lockscreen.
Change-Id: I36dd49363e81df5a260b10695d90aa9d8c70a45a
(cherry picked from commit 0e17480b1f15635aa9ed05e662deb0aba855bdfa)
diff --git a/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java b/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java
index c9d4957..bd91333 100644
--- a/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java
+++ b/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java
@@ -47,7 +47,7 @@
     // This is to avoid destroying then recreating render context in a very short time.
     private static final int DELAY_FINISH_RENDERING = 1000;
     private static final int INTERVAL_WAIT_FOR_RENDERING = 100;
-    private static final int PATIENCE_WAIT_FOR_RENDERING = 5;
+    private static final int PATIENCE_WAIT_FOR_RENDERING = 10;
     private HandlerThread mWorker;
 
     @Override
@@ -124,10 +124,10 @@
 
         @Override
         public void onAmbientModeChanged(boolean inAmbientMode, long animationDuration) {
-            long duration = mNeedTransition || animationDuration != 0 ? animationDuration : 0;
+            if (!mNeedTransition) return;
             mWorker.getThreadHandler().post(
-                    () -> mRenderer.updateAmbientMode(inAmbientMode, duration));
-            if (inAmbientMode && duration == 0) {
+                    () -> mRenderer.updateAmbientMode(inAmbientMode, animationDuration));
+            if (inAmbientMode && animationDuration == 0) {
                 // This means that we are transiting from home to aod, to avoid
                 // race condition between window visibility and transition,
                 // we don't return until the transition is finished. See b/136643341.
diff --git a/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageRevealHelper.java b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageRevealHelper.java
index 45e97b3..b154e66 100644
--- a/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageRevealHelper.java
+++ b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageRevealHelper.java
@@ -65,7 +65,7 @@
             @Override
             public void onAnimationStart(Animator animation) {
                 if (mRevealListener != null) {
-                    mRevealListener.onRevealStart();
+                    mRevealListener.onRevealStart(true /* animate */);
                 }
             }
         });
@@ -73,7 +73,7 @@
 
     private void animate() {
         mAnimator.cancel();
-        mAnimator.setFloatValues(mReveal, !mAwake ? MIN_REVEAL : MAX_REVEAL);
+        mAnimator.setFloatValues(mReveal, mAwake ? MAX_REVEAL : MIN_REVEAL);
         mAnimator.start();
     }
 
@@ -84,12 +84,11 @@
     void updateAwake(boolean awake, long duration) {
         mAwake = awake;
         mAnimator.setDuration(duration);
-        if (!mAwake && duration == 0) {
-            // We are transiting from home to aod,
-            // since main thread is waiting for rendering finished, we only need draw
-            // the last state directly, which is a black screen.
-            mReveal = MIN_REVEAL;
-            mRevealListener.onRevealStart();
+        if (duration == 0) {
+            // We are transiting from home to aod or aod to home directly,
+            // we don't need to do transition in these cases.
+            mReveal = mAwake ? MAX_REVEAL : MIN_REVEAL;
+            mRevealListener.onRevealStart(false /* animate */);
             mRevealListener.onRevealStateChanged();
             mRevealListener.onRevealEnd();
         } else {
@@ -110,7 +109,7 @@
         /**
          * Called back while reveal starts.
          */
-        void onRevealStart();
+        void onRevealStart(boolean animate);
 
         /**
          * Called back while reveal ends.
diff --git a/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageWallpaperRenderer.java b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageWallpaperRenderer.java
index 93d8dd6..7b22a49 100644
--- a/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageWallpaperRenderer.java
+++ b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageWallpaperRenderer.java
@@ -24,6 +24,7 @@
 
 import android.app.WallpaperManager;
 import android.content.Context;
+import android.content.res.Configuration;
 import android.graphics.Bitmap;
 import android.graphics.Rect;
 import android.util.Log;
@@ -70,7 +71,14 @@
         DisplayInfo displayInfo = new DisplayInfo();
         WindowManager wm = context.getSystemService(WindowManager.class);
         wm.getDefaultDisplay().getDisplayInfo(displayInfo);
-        mScissor = new Rect(0, 0, displayInfo.logicalWidth, displayInfo.logicalHeight);
+
+        // We only do transition in portrait currently, b/137962047.
+        int orientation = context.getResources().getConfiguration().orientation;
+        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
+            mScissor = new Rect(0, 0, displayInfo.logicalWidth, displayInfo.logicalHeight);
+        } else {
+            mScissor = new Rect(0, 0, displayInfo.logicalHeight, displayInfo.logicalWidth);
+        }
 
         mProxy = proxy;
         mProgram = new ImageGLProgram(context);
@@ -179,20 +187,24 @@
     }
 
     @Override
-    public void onRevealStart() {
-        mScissorMode = true;
-        // Use current display area of texture.
-        mWallpaper.adjustTextureCoordinates(mSurfaceSize, mScissor, mXOffset, mYOffset);
+    public void onRevealStart(boolean animate) {
+        if (animate) {
+            mScissorMode = true;
+            // Use current display area of texture.
+            mWallpaper.adjustTextureCoordinates(mSurfaceSize, mScissor, mXOffset, mYOffset);
+        }
         mProxy.preRender();
     }
 
     @Override
     public void onRevealEnd() {
-        mScissorMode = false;
-        // reset texture coordinates to use full texture.
-        mWallpaper.adjustTextureCoordinates(null, null, 0, 0);
-        // We need draw full texture back before finishing render.
-        mProxy.requestRender();
+        if (mScissorMode) {
+            mScissorMode = false;
+            // reset texture coordinates to use full texture.
+            mWallpaper.adjustTextureCoordinates(null, null, 0, 0);
+            // We need draw full texture back before finishing render.
+            mProxy.requestRender();
+        }
         mProxy.postRender();
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
index 7d6799b..c6de829 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
@@ -482,9 +482,12 @@
             WallpaperInfo info = wallpaperManager.getWallpaperInfo(UserHandle.USER_CURRENT);
             final boolean deviceSupportsAodWallpaper = mContext.getResources().getBoolean(
                     com.android.internal.R.bool.config_dozeSupportsAodWallpaper);
+            final boolean imageWallpaperInAmbient =
+                    !DozeParameters.getInstance(mContext).getDisplayNeedsBlanking();
             // If WallpaperInfo is null, it must be ImageWallpaper.
             final boolean supportsAmbientMode = deviceSupportsAodWallpaper
-                    && (info == null || info.supportsAmbientMode());
+                    && ((info == null && imageWallpaperInAmbient)
+                        || (info != null && info.supportsAmbientMode()));
 
             mStatusBarWindowController.setWallpaperSupportsAmbientMode(supportsAmbientMode);
             mScrimController.setWallpaperSupportsAmbientMode(supportsAmbientMode);