Hold reference to and close both ends of PFD

Speculative fix for bug attached to this CL. Crash is occurring as a
result of a file descriptor attempting to be closed while in use. Crash
seems to be due to a ParcelFileDescriptor in statsd codebase.
ParcelFileDescriptor in StatsCompanionService may be garbage collected
before a reference of it is grabbed. Initiating ParcelFileDescriptor as
an instance variable in the StatsCompanionService class and closing both
ends of the pipe after writing proto data.

Bug: 267350818
Test: statsd_test
Test: CtsStatsdHostTestCases
Change-Id: I412eba330dd98f755ef5e9388c2a40693b36809a
diff --git a/service/java/com/android/server/stats/StatsCompanionService.java b/service/java/com/android/server/stats/StatsCompanionService.java
index f8b9400..636689d 100644
--- a/service/java/com/android/server/stats/StatsCompanionService.java
+++ b/service/java/com/android/server/stats/StatsCompanionService.java
@@ -207,6 +207,7 @@
         backgroundThread.start();
         Handler handler = new Handler(backgroundThread.getLooper());
         handler.post(() -> {
+            if (DEBUG) Log.d(TAG, "Start thread for sending uid map data.");
             UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
             PackageManager pm = context.getPackageManager();
             final List<UserHandle> users = um.getUserHandles(true);
@@ -227,8 +228,7 @@
             } catch (IOException e) {
                 Log.e(TAG, "Failed to close the read side of the pipe.", e);
             }
-            final ParcelFileDescriptor writeFd = fds[1];
-            FileOutputStream fout = new ParcelFileDescriptor.AutoCloseOutputStream(writeFd);
+            FileOutputStream fout = new ParcelFileDescriptor.AutoCloseOutputStream(fds[1]);
             try {
                 ProtoOutputStream output = new ProtoOutputStream(fout);
                 int numRecords = 0;
@@ -283,9 +283,8 @@
                     Log.d(TAG, "Sent data for " + numRecords + " apps");
                 }
             } finally {
-                FileUtils.closeQuietly(fout);
+                if (DEBUG) Log.d(TAG, "End thread for sending uid map data.");
                 backgroundThread.quit();
-                backgroundThread.interrupt();
             }
         });
     }