Set HAS_CONTENT field from the app after writing audio content.

In change-id I93e4b467acdefe339fa70dd751ea05f195c32e71 content voicemail
content provider was modified to no more automatically set the
has_content field. The app now needs to set this field itself after
writing the content.

bug: 5147190
Change-Id: If3c66a86803f7587677c2ec09965b89f31f6268e
diff --git a/samples/VoicemailProviderDemo/src/com/example/android/voicemail/AddVoicemailActivity.java b/samples/VoicemailProviderDemo/src/com/example/android/voicemail/AddVoicemailActivity.java
index 121840a..2adb0c4 100644
--- a/samples/VoicemailProviderDemo/src/com/example/android/voicemail/AddVoicemailActivity.java
+++ b/samples/VoicemailProviderDemo/src/com/example/android/voicemail/AddVoicemailActivity.java
@@ -206,13 +206,10 @@
             Uri newVoicemailUri = mVoicemailProviderHelper.insert(voicemail);
             logger.i("Inserted new voicemail URI: " + newVoicemailUri);
             if (inputAudioStream != null) {
-                OutputStream outputStream = null;
                 try {
-                    outputStream = mVoicemailProviderHelper.setVoicemailContent(
-                            newVoicemailUri, getContentResolver().getType(recordingUri));
-                    copyStreamData(inputAudioStream, outputStream);
+                    mVoicemailProviderHelper.setVoicemailContent(newVoicemailUri, inputAudioStream,
+                            getContentResolver().getType(recordingUri));
                 } finally {
-                    CloseUtils.closeQuietly(outputStream);
                     CloseUtils.closeQuietly(inputAudioStream);
                 }
             }
@@ -228,13 +225,5 @@
             }
         }
 
-        private void copyStreamData(InputStream in, OutputStream out) throws IOException {
-            // Copy 8K chunk at a time.
-            byte[] data = new byte[8 * 1024];
-            int numBytes;
-            while ((numBytes = in.read(data)) > 0) {
-                out.write(data, 0, numBytes);
-            }
-        }
     }
 }
diff --git a/samples/VoicemailProviderDemo/src/com/example/android/voicemail/common/core/VoicemailProviderHelper.java b/samples/VoicemailProviderDemo/src/com/example/android/voicemail/common/core/VoicemailProviderHelper.java
index 9cb6a3b..918aa4a 100644
--- a/samples/VoicemailProviderDemo/src/com/example/android/voicemail/common/core/VoicemailProviderHelper.java
+++ b/samples/VoicemailProviderDemo/src/com/example/android/voicemail/common/core/VoicemailProviderHelper.java
@@ -21,7 +21,7 @@
 import android.net.Uri;
 
 import java.io.IOException;
-import java.io.OutputStream;
+import java.io.InputStream;
 import java.util.List;
 
 /**
@@ -99,14 +99,25 @@
     public int update(Uri uri, Voicemail voicemail);
 
     /**
-     * Get the OutputStream to write the voicemail content with the given mime type.
+     * Sets the voicemail content from the supplied input stream.
      * <p>
-     * <b>Remember to close the OutputStream after you're done writing.</b>
+     * The inputStream is owned by the caller and must be closed by it as usual after the call has
+     * returned.
      *
      * @throws IOException if there is a problem creating the file or no voicemail is found matching
      *             the given Uri
      */
-    public OutputStream setVoicemailContent(Uri voicemailUri, String mimeType) throws IOException;
+    public void setVoicemailContent(Uri voicemailUri, InputStream inputStream, String mimeType)
+            throws IOException;
+
+    /**
+     * Sets the voicemail content from the supplied byte array.
+     *
+     * @throws IOException if there is a problem creating the file or no voicemail is found matching
+     *             the given Uri
+     */
+    public void setVoicemailContent(Uri voicemailUri, byte[] inputBytes, String mimeType)
+            throws IOException;
 
     /**
      * Fetch all the voicemails accessible to this voicemail content provider.
diff --git a/samples/VoicemailProviderDemo/src/com/example/android/voicemail/common/core/VoicemailProviderHelpers.java b/samples/VoicemailProviderDemo/src/com/example/android/voicemail/common/core/VoicemailProviderHelpers.java
index 1840462..27c7f69 100644
--- a/samples/VoicemailProviderDemo/src/com/example/android/voicemail/common/core/VoicemailProviderHelpers.java
+++ b/samples/VoicemailProviderDemo/src/com/example/android/voicemail/common/core/VoicemailProviderHelpers.java
@@ -30,6 +30,7 @@
 import android.provider.VoicemailContract.Voicemails;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.List;
@@ -114,16 +115,45 @@
     }
 
     @Override
-    public OutputStream setVoicemailContent(Uri voicemailUri, String mimeType) throws IOException {
+    public void setVoicemailContent(Uri voicemailUri, InputStream inputStream, String mimeType)
+            throws IOException {
+        setVoicemailContent(voicemailUri, null, inputStream, mimeType);
+    }
+
+    @Override
+    public void setVoicemailContent(Uri voicemailUri, byte[] inputBytes, String mimeType)
+            throws IOException {
+        setVoicemailContent(voicemailUri, inputBytes, null, mimeType);
+    }
+
+    private void setVoicemailContent(Uri voicemailUri, byte[] inputBytes, InputStream inputStream,
+            String mimeType) throws IOException {
+        if (inputBytes != null && inputStream != null) {
+            throw new IllegalArgumentException("Both inputBytes & inputStream non-null. Don't" +
+                    " know which one to use.");
+        }
+
+        logger.d(String.format("Writing new voicemail content: %s", voicemailUri));
+        OutputStream outputStream = null;
+        try {
+            outputStream = mContentResolver.openOutputStream(voicemailUri);
+            if (inputBytes != null) {
+                outputStream.write(inputBytes);
+            } else if (inputStream != null) {
+                copyStreamData(inputStream, outputStream);
+            }
+        } finally {
+            CloseUtils.closeQuietly(outputStream);
+        }
+        // Update mime_type & has_content after we are done with file update.
         ContentValues values = new ContentValues();
         values.put(Voicemails.MIME_TYPE, mimeType);
+        values.put(Voicemails.HAS_CONTENT, true);
         int updatedCount = mContentResolver.update(voicemailUri, values, null, null);
         if (updatedCount != 1) {
             throw new IOException("Updating voicemail should have updated 1 row, was: "
                     + updatedCount);
         }
-        logger.d(String.format("Writing new voicemail content: %s", voicemailUri));
-        return mContentResolver.openOutputStream(voicemailUri);
     }
 
     @Override
@@ -288,4 +318,13 @@
         }
         return contentValues;
     }
+
+    private void copyStreamData(InputStream in, OutputStream out) throws IOException {
+        byte[] data = new byte[8 * 1024];
+        int numBytes;
+        while ((numBytes = in.read(data)) > 0) {
+            out.write(data, 0, numBytes);
+        }
+
+    }
 }