Fix flakey vcard test

When the test fails, I see an error from MemoryFile, which is used
in ParcelFileDescriptor.fromData().  However the thing is this
method is deprecated -- so I rewrote it to use a pipe instead.

Bug 27335482

Change-Id: Ib75d9e93ac2610d3145889fe269abeec3d7c3dee
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java
index 6414fbd..4ba7a81 100644
--- a/src/com/android/providers/contacts/ContactsProvider2.java
+++ b/src/com/android/providers/contacts/ContactsProvider2.java
@@ -191,7 +191,9 @@
 import java.io.File;
 import java.io.FileDescriptor;
 import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
@@ -9092,10 +9094,24 @@
         try {
             stream.flush();
 
-            final byte[] byteData = stream.toByteArray();
-            return makeAssetFileDescriptor(
-                    ParcelFileDescriptor.fromData(byteData, CONTACT_MEMORY_FILE_NAME),
-                    byteData.length);
+            final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
+            final FileDescriptor outFd = fds[1].getFileDescriptor();
+
+            AsyncTask<Object, Object, Object> task = new AsyncTask<Object, Object, Object>() {
+                @Override
+                protected Object doInBackground(Object... params) {
+                    try (FileOutputStream fout = new FileOutputStream(outFd)) {
+                        fout.write(stream.toByteArray());
+                    } catch (IOException|RuntimeException e) {
+                        Log.w(TAG, "Failure closing pipe", e);
+                    }
+                    IoUtils.closeQuietly(outFd);
+                    return null;
+                }
+            };
+            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Object[])null);
+
+            return makeAssetFileDescriptor(fds[0]);
         } catch (IOException e) {
             Log.w(TAG, "Problem writing stream into an ParcelFileDescriptor: " + e.toString());
             return null;