Merge "Pause and resume ActivityView when visibility changes" into klp-modular-dev
diff --git a/core/java/android/app/ActivityView.java b/core/java/android/app/ActivityView.java
index a810134..097c64e 100644
--- a/core/java/android/app/ActivityView.java
+++ b/core/java/android/app/ActivityView.java
@@ -33,6 +33,7 @@
 import android.view.Surface;
 import android.view.TextureView;
 import android.view.TextureView.SurfaceTextureListener;
+import android.view.View;
 import android.view.ViewGroup;
 import android.view.WindowManager;
 import dalvik.system.CloseGuard;
@@ -51,6 +52,7 @@
     private int mWidth;
     private int mHeight;
     private Surface mSurface;
+    private int mLastVisibility;
 
     // Only one IIntentSender or Intent may be queued at a time. Most recent one wins.
     IIntentSender mQueuedPendingIntent;
@@ -95,6 +97,8 @@
         mMetrics = new DisplayMetrics();
         wm.getDefaultDisplay().getMetrics(mMetrics);
 
+        mLastVisibility = getVisibility();
+
         if (DEBUG) Log.v(TAG, "ctor()");
     }
 
@@ -103,6 +107,26 @@
         mTextureView.layout(0, 0, r - l, b - t);
     }
 
+    @Override
+    protected void onVisibilityChanged(View changedView, int visibility) {
+        super.onVisibilityChanged(changedView, visibility);
+
+        if (mSurface != null) {
+            try {
+                if (visibility == View.GONE) {
+                    mActivityContainer.setSurface(null, mWidth, mHeight, mMetrics.densityDpi);
+                } else if (mLastVisibility == View.GONE) {
+                    // Don't change surface when going between View.VISIBLE and View.INVISIBLE.
+                    mActivityContainer.setSurface(mSurface, mWidth, mHeight, mMetrics.densityDpi);
+                }
+            } catch (RemoteException e) {
+                throw new RuntimeException(
+                        "ActivityView: Unable to set surface of ActivityContainer. " + e);
+            }
+        }
+        mLastVisibility = visibility;
+    }
+
     private boolean injectInputEvent(InputEvent event) {
         return mActivityContainer != null && mActivityContainer.injectEvent(event);
     }
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index 5e45917..e942afb 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -3187,9 +3187,9 @@
             } else {
                 mContainerState = CONTAINER_STATE_NO_SURFACE;
                 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
-//                if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
-//                    mStack.startPausingLocked(false, true);
-//                }
+                if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
+                    mStack.startPausingLocked(false, true);
+                }
             }
 
             setSurfaceIfReady();
@@ -3199,6 +3199,11 @@
         }
 
         @Override
+        boolean isAttached() {
+            return mSurface != null && super.isAttached();
+        }
+
+        @Override
         void setDrawn() {
             synchronized (mService) {
                 mDrawn = true;