Fixed issue with turn-screen-on activities

Regression introduced by ag/2147938 where we set the visibility of an
app to false after pausing because it might have been previously
defered. However, the change did it for all activities regardless of if
it was previously deferred or not. We now only do the visibility change
to false if we previouly deferred.

Change-Id: Ifde46587e1ddf38304b73cec45c56f0acd955f37
Fixes: 37657105
Test: cts.ActivityManagerActivityVisibilityTests#testTurnScreenOnActivity
(cherry picked from commit ec95064ded280616277be44cd5460f07c43631ef)
diff --git a/services/core/java/com/android/server/am/ActivityRecord.java b/services/core/java/com/android/server/am/ActivityRecord.java
index eeedab8..3e0734f 100644
--- a/services/core/java/com/android/server/am/ActivityRecord.java
+++ b/services/core/java/com/android/server/am/ActivityRecord.java
@@ -274,11 +274,13 @@
                                         // completed
     boolean preserveWindowOnDeferredRelaunch; // activity windows are preserved on deferred relaunch
     int configChangeFlags;  // which config values have changed
-    boolean keysPaused;     // has key dispatching been paused for it?
+    private boolean keysPaused;     // has key dispatching been paused for it?
     int launchMode;         // the launch mode activity attribute.
     boolean visible;        // does this activity's window need to be shown?
     boolean visibleIgnoringKeyguard; // is this activity visible, ignoring the fact that Keyguard
                                      // might hide this activity?
+    private boolean mDeferHidingClient; // If true we told WM to defer reporting to the client
+                                        // process that it is hidden.
     boolean sleeping;       // have we told the activity to sleep?
     boolean nowVisible;     // is this activity's window visible?
     boolean idle;           // has the activity gone idle?
@@ -523,6 +525,9 @@
                     else TimeUtils.formatDuration(lastVisibleTime, now, pw);
                     pw.println();
         }
+        if (mDeferHidingClient) {
+            pw.println(prefix + "mDeferHidingClient=" + mDeferHidingClient);
+        }
         if (deferRelaunchUntilPaused || configChangeFlags != 0) {
             pw.print(prefix); pw.print("deferRelaunchUntilPaused="); pw.print(deferRelaunchUntilPaused);
                     pw.print(" configChangeFlags=");
@@ -1567,22 +1572,31 @@
         return mWindowContainerController.screenshotApplications(getDisplayId(), w, h, scale);
     }
 
-    void setVisibility(boolean visible) {
-        mWindowContainerController.setVisibility(visible, false /* deferHidingClient */);
+    void setDeferHidingClient(boolean deferHidingClient) {
+        if (mDeferHidingClient == deferHidingClient) {
+            return;
+        }
+        mDeferHidingClient = deferHidingClient;
+        if (!mDeferHidingClient && !visible) {
+            // Hiding the client is no longer deferred and the app isn't visible still, go ahead and
+            // update the visibility.
+            setVisibility(false);
+        }
     }
 
-    void setVisible(boolean newVisible) {
-        setVisible(newVisible, false /* deferHidingClient */);
+    void setVisibility(boolean visible) {
+        mWindowContainerController.setVisibility(visible, mDeferHidingClient);
     }
 
     // TODO: Look into merging with #setVisibility()
-    void setVisible(boolean newVisible, boolean deferHidingClient) {
+    void setVisible(boolean newVisible) {
         visible = newVisible;
+        mDeferHidingClient = !visible && mDeferHidingClient;
         if (!visible && mUpdateTaskThumbnailWhenHidden) {
             updateThumbnailLocked(screenshotActivityLocked(), null /* description */);
             mUpdateTaskThumbnailWhenHidden = false;
         }
-        mWindowContainerController.setVisibility(visible, deferHidingClient);
+        setVisibility(visible);
         final ArrayList<ActivityContainer> containers = mChildContainers;
         for (int containerNdx = containers.size() - 1; containerNdx >= 0; --containerNdx) {
             final ActivityContainer container = containers.get(containerNdx);
diff --git a/services/core/java/com/android/server/am/ActivityStack.java b/services/core/java/com/android/server/am/ActivityStack.java
index 1e8a587..728a3b9 100644
--- a/services/core/java/com/android/server/am/ActivityStack.java
+++ b/services/core/java/com/android/server/am/ActivityStack.java
@@ -1406,10 +1406,10 @@
                     prev.state = STOPPING;
                 } else if ((!prev.visible && !hasVisibleBehindActivity())
                         || mService.isSleepingOrShuttingDownLocked()) {
+                    // Clear out any deferred client hide we might currently have.
+                    prev.setDeferHidingClient(false);
                     // If we were visible then resumeTopActivities will release resources before
-                    // stopping. Also, set visibility to false to flush any client hide that might have
-                    // been deferred.
-                    prev.setVisibility(false);
+                    // stopping.
                     addToStopping(prev, true /* scheduleIdle */, false /* idleDelayed */);
                 }
             } else {
@@ -2030,7 +2030,8 @@
             // or stopping. This gives it a chance to enter Pip in onPause().
             final boolean deferHidingClient = canEnterPictureInPicture
                     && r.state != STOPPING && r.state != STOPPED;
-            r.setVisible(false, deferHidingClient);
+            r.setDeferHidingClient(deferHidingClient);
+            r.setVisible(false);
 
             switch (r.state) {
                 case STOPPING: