aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMakoto Onuki <omakoto@google.com>2016-03-15 13:43:35 -0700
committerMakoto Onuki <omakoto@google.com>2016-03-15 13:43:35 -0700
commit4d089e6ae79b011abbd6495b5181674d91561188 (patch)
tree2882e984be532d28daa95130143574140d3f907f
parent63b0203a07ea9f8b61834ea6dec66562b961f410 (diff)
downloadContactsProvider-4d089e6ae79b011abbd6495b5181674d91561188.tar.gz
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
-rw-r--r--src/com/android/providers/contacts/ContactsProvider2.java24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java
index 6414fbdf..4ba7a818 100644
--- a/src/com/android/providers/contacts/ContactsProvider2.java
+++ b/src/com/android/providers/contacts/ContactsProvider2.java
@@ -191,7 +191,9 @@ import java.io.ByteArrayOutputStream;
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 @@ public class ContactsProvider2 extends AbstractContactsProvider
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;