Support sharing with image/video/audio/vcard and text fully

ACTION_SEND and ACTION_SEND_MULTIPLE support image, video, audio, text
and vcard files.

Test: Manual

Change-Id: I1c08070f89877140aba9087717bb663ee2990467
Signed-off-by: Taesu Lee <taesu82.lee@samsung.com>
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index e23fcc0..bd77a49 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -210,20 +210,16 @@
             <intent-filter
                 android:label="@string/share_intent_label">
                 <action android:name="android.intent.action.SEND" />
+                <action android:name="android.intent.action.SEND_MULTIPLE" />
                 <category android:name="android.intent.category.DEFAULT" />
                 <data android:mimeType="text/plain" />
                 <data android:mimeType="text/x-vCard" />
                 <data android:mimeType="text/x-vcard" />
                 <data android:mimeType="image/*" />
                 <data android:mimeType="audio/*" />
+                <data android:mimeType="video/*" />
                 <data android:mimeType="application/ogg" />
             </intent-filter>
-            <intent-filter
-                    android:label="@string/share_intent_label">
-                <action android:name="android.intent.action.SEND_MULTIPLE" />
-                <category android:name="android.intent.category.DEFAULT" />
-                <data android:mimeType="image/*" />
-            </intent-filter>
         </activity>
 
         <!-- People & Options -->
diff --git a/src/com/android/messaging/ui/conversationlist/ShareIntentActivity.java b/src/com/android/messaging/ui/conversationlist/ShareIntentActivity.java
index 1c91e46..25d5ea3 100644
--- a/src/com/android/messaging/ui/conversationlist/ShareIntentActivity.java
+++ b/src/com/android/messaging/ui/conversationlist/ShareIntentActivity.java
@@ -24,6 +24,8 @@
 import android.os.Bundle;
 import android.text.TextUtils;
 
+import androidx.collection.ArrayMap;
+
 import com.android.messaging.Factory;
 import com.android.messaging.datamodel.data.ConversationListItemData;
 import com.android.messaging.datamodel.data.MessageData;
@@ -37,8 +39,13 @@
 import com.android.messaging.util.FileUtil;
 import com.android.messaging.util.UriUtil;
 
+import java.io.BufferedReader;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Map;
 
 public class ShareIntentActivity extends BaseBugleActivity implements
         ShareIntentFragment.HostInterface {
@@ -89,51 +96,68 @@
                         contentUri, intent.getType(), contentType));
             }
             if (ContentType.TEXT_PLAIN.equals(contentType)) {
-                final String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
-                if (sharedText != null) {
-                    mDraftMessage = MessageData.createSharedMessage(sharedText);
-                } else {
-                    mDraftMessage = null;
+                String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
+                if (sharedText == null) {
+                    // Try to get text string from content uri.
+                    sharedText = getTextStringFromContentUri(contentUri);
                 }
-            } else if (ContentType.isImageType(contentType) ||
-                    ContentType.isVCardType(contentType) ||
-                    ContentType.isAudioType(contentType) ||
-                    ContentType.isVideoType(contentType)) {
+                mDraftMessage =
+                        sharedText != null ? MessageData.createSharedMessage(sharedText) : null;
+            } else if (ContentType.isMediaType(contentType)) {
                 if (contentUri != null) {
                     mDraftMessage = MessageData.createSharedMessage(null);
-                    addSharedImagePartToDraft(contentType, contentUri);
+                    addSharedPartToDraft(contentType, contentUri);
                 } else {
                     mDraftMessage = null;
                 }
             } else {
                 // Unsupported content type.
-                Assert.fail("Unsupported shared content type for " + contentUri + ": " + contentType
-                        + " (" + intent.getType() + ")");
+                LogUtil.e(LogUtil.BUGLE_TAG, "Unsupported shared content type for " + contentUri
+                        + ": " + contentType + " (" + intent.getType() + ")");
+                mDraftMessage = null;
             }
         } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
             final String contentType = intent.getType();
-            if (ContentType.isImageType(contentType)) {
-                // Handle sharing multiple images.
-                final ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(
-                        Intent.EXTRA_STREAM);
-                if (imageUris != null && imageUris.size() > 0) {
-                    mDraftMessage = MessageData.createSharedMessage(null);
-                    for (final Uri imageUri : imageUris) {
-                        if (UriUtil.isFileUri(imageUri)) {
-                            LogUtil.i(
-                                LogUtil.BUGLE_TAG,
+            // Handle sharing multiple contents.
+            final ArrayList<Uri> uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
+            if (uris != null && !uris.isEmpty()) {
+                ArrayMap<Uri, String> uriMap = new ArrayMap<Uri, String>();
+                StringBuffer strBuffer = new StringBuffer();
+                for (final Uri uri : uris) {
+                    if (UriUtil.isFileUri(uri)) {
+                        LogUtil.i(LogUtil.BUGLE_TAG,
                                 "Ignoring attachment from file URI which are no longer supported.");
-                            continue;
-                        }
-                        final String actualContentType = extractContentType(imageUri, contentType);
-                        addSharedImagePartToDraft(actualContentType, imageUri);
+                        continue;
                     }
-                } else {
-                    mDraftMessage = null;
+                    final String actualContentType = extractContentType(uri, contentType);
+                    if (ContentType.TEXT_PLAIN.equals(actualContentType)) {
+                        // Try to get text string from content uri.
+                        String sharedText = getTextStringFromContentUri(uri);
+                        if (sharedText != null) {
+                            if (strBuffer.length() > 0) {
+                                strBuffer.append("\n");
+                            }
+                            strBuffer.append(sharedText);
+                        }
+                    } else if (ContentType.isMediaType(actualContentType)) {
+                        uriMap.put(uri, actualContentType);
+                    } else {
+                        // Unsupported content type.
+                        LogUtil.e(LogUtil.BUGLE_TAG, "Unsupported shared content type for " + uri
+                                + ": " + actualContentType);
+                    }
+                }
+
+                if (strBuffer.length() > 0 || !uriMap.isEmpty()) {
+                    mDraftMessage = MessageData.createSharedMessage(strBuffer.toString());
+                    for (final Map.Entry<Uri, String> e : uriMap.entrySet()) {
+                        addSharedPartToDraft(e.getValue(), e.getKey());
+                    }
                 }
             } else {
-                // Unsupported content type.
-                Assert.fail("Unsupported shared content type: " + contentType);
+                // No EXTRA_STREAM.
+                LogUtil.e(LogUtil.BUGLE_TAG, "No shared URI.");
+                mDraftMessage = null;
             }
         } else {
             // Unsupported action.
@@ -141,6 +165,40 @@
         }
     }
 
+    private static String getTextStringFromContentUri(final Uri contentUri) {
+        if (contentUri == null) {
+            return null;
+        }
+        final ContentResolver resolver = Factory.get().getApplicationContext().getContentResolver();
+        BufferedReader reader = null;
+        try {
+            final InputStream in = resolver.openInputStream(contentUri);
+            reader = new BufferedReader(new InputStreamReader(in));
+            String line = reader.readLine();
+            if (line == null) {
+                return null;
+            }
+            StringBuffer strBuffer = new StringBuffer(line);
+            while ((line = reader.readLine()) != null) {
+                strBuffer.append("\n").append(line);
+            }
+            return strBuffer.toString();
+        } catch (FileNotFoundException e) {
+            LogUtil.w(LogUtil.BUGLE_TAG, "Can not find contentUri " + contentUri);
+        } catch (IOException e) {
+            LogUtil.w(LogUtil.BUGLE_TAG, "Can not read contentUri", e);
+        } finally {
+            try {
+                if (reader != null) {
+                    reader.close();
+                }
+            } catch (IOException e) {
+                // Ignore
+            }
+        }
+        return null;
+    }
+
     private static String extractContentType(final Uri uri, final String contentType) {
         if (uri == null) {
             return contentType;
@@ -171,12 +229,12 @@
         return contentType;
     }
 
-    private void addSharedImagePartToDraft(final String contentType, final Uri imageUri) {
-        if (FileUtil.isInPrivateDir(imageUri)) {
-            Assert.fail("Cannot send private file " + imageUri.toString());
+    private void addSharedPartToDraft(final String contentType, final Uri uri) {
+        if (FileUtil.isInPrivateDir(uri)) {
+            Assert.fail("Cannot send private file " + uri.toString());
         } else {
-            mDraftMessage.addPart(PendingAttachmentData.createPendingAttachmentData(contentType,
-                    imageUri));
+            mDraftMessage.addPart(
+                    PendingAttachmentData.createPendingAttachmentData(contentType, uri));
         }
     }