Reduce object allocations in WM in some frequently called methods

With the use of lambdas to get all windows in the window container
hierarchy, we need to be careful in frequently called code paths to
make sure the number of objects we allocate isn't crazy. This CL
converts some commonly called code paths that use lambda to use a
method reference for the lambda so we only need to allocate once vs.
each time the code path is executed.

Test: Perform some common operations on the phone and make sure
      the object allocations that show-up in Allocator Tracker for
      window manager seems reasonable

Change-Id: Ie0f245980de96ec68a4e62e76130db7d98c3f7d9
diff --git a/services/core/java/com/android/server/wm/RootWindowContainer.java b/services/core/java/com/android/server/wm/RootWindowContainer.java
index ee6d4ea..349740b 100644
--- a/services/core/java/com/android/server/wm/RootWindowContainer.java
+++ b/services/core/java/com/android/server/wm/RootWindowContainer.java
@@ -40,6 +40,7 @@
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.function.Consumer;
 
 import static android.app.AppOpsManager.MODE_ALLOWED;
 import static android.app.AppOpsManager.MODE_DEFAULT;
@@ -129,6 +130,23 @@
     private final WindowLayersController mLayersController;
     final WallpaperController mWallpaperController;
 
+    private String mCloseSystemDialogsReason;
+    private final Consumer<WindowState> mCloseSystemDialogsConsumer = w -> {
+        if (w.mHasSurface) {
+            try {
+                w.mClient.closeSystemDialogs(mCloseSystemDialogsReason);
+            } catch (RemoteException e) {
+            }
+        }
+    };
+
+    private static final Consumer<WindowState> sRemoveReplacedWindowsConsumer = w -> {
+        final AppWindowToken aToken = w.mAppToken;
+        if (aToken != null) {
+            aToken.removeReplacedWindowIfNeeded(w);
+        }
+    };
+
     RootWindowContainer(WindowManagerService service) {
         mService = service;
         mLayersController = new WindowLayersController(mService);
@@ -376,26 +394,15 @@
     }
 
     void closeSystemDialogs(String reason) {
-        forAllWindows((w) -> {
-            if (w.mHasSurface) {
-                try {
-                    w.mClient.closeSystemDialogs(reason);
-                } catch (RemoteException e) {
-                }
-            }
-        }, false /* traverseTopToBottom */);
+        mCloseSystemDialogsReason = reason;
+        forAllWindows(mCloseSystemDialogsConsumer, false /* traverseTopToBottom */);
     }
 
     void removeReplacedWindows() {
         if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION removeReplacedWindows");
         mService.openSurfaceTransaction();
         try {
-            forAllWindows((w) -> {
-                final AppWindowToken aToken = w.mAppToken;
-                if (aToken != null) {
-                    aToken.removeReplacedWindowIfNeeded(w);
-                }
-            }, true /* traverseTopToBottom */);
+            forAllWindows(sRemoveReplacedWindowsConsumer, true /* traverseTopToBottom */);
         } finally {
             mService.closeSurfaceTransaction();
             if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION removeReplacedWindows");