Merge "Support sharing with image/video/audio/vcard and text fully" am: e8827131fc

Change-Id: Ia5d01a9586d42ae49685bbf9e8d7d1ece1a2c480
This commit is contained in:
Automerger Merge Worker
2020-02-07 03:30:25 +00:00
2 changed files with 95 additions and 41 deletions
+2 -6
View File
@@ -210,20 +210,16 @@
<intent-filter <intent-filter
android:label="@string/share_intent_label"> android:label="@string/share_intent_label">
<action android:name="android.intent.action.SEND" /> <action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" /> <data android:mimeType="text/plain" />
<data android:mimeType="text/x-vCard" /> <data android:mimeType="text/x-vCard" />
<data android:mimeType="text/x-vcard" /> <data android:mimeType="text/x-vcard" />
<data android:mimeType="image/*" /> <data android:mimeType="image/*" />
<data android:mimeType="audio/*" /> <data android:mimeType="audio/*" />
<data android:mimeType="video/*" />
<data android:mimeType="application/ogg" /> <data android:mimeType="application/ogg" />
</intent-filter> </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> </activity>
<!-- People & Options --> <!-- People & Options -->
@@ -24,6 +24,8 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.collection.ArrayMap;
import com.android.messaging.Factory; import com.android.messaging.Factory;
import com.android.messaging.datamodel.data.ConversationListItemData; import com.android.messaging.datamodel.data.ConversationListItemData;
import com.android.messaging.datamodel.data.MessageData; import com.android.messaging.datamodel.data.MessageData;
@@ -37,8 +39,13 @@ import com.android.messaging.util.MediaMetadataRetrieverWrapper;
import com.android.messaging.util.FileUtil; import com.android.messaging.util.FileUtil;
import com.android.messaging.util.UriUtil; 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.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map;
public class ShareIntentActivity extends BaseBugleActivity implements public class ShareIntentActivity extends BaseBugleActivity implements
ShareIntentFragment.HostInterface { ShareIntentFragment.HostInterface {
@@ -89,51 +96,68 @@ public class ShareIntentActivity extends BaseBugleActivity implements
contentUri, intent.getType(), contentType)); contentUri, intent.getType(), contentType));
} }
if (ContentType.TEXT_PLAIN.equals(contentType)) { if (ContentType.TEXT_PLAIN.equals(contentType)) {
final String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) { if (sharedText == null) {
mDraftMessage = MessageData.createSharedMessage(sharedText); // Try to get text string from content uri.
} else { sharedText = getTextStringFromContentUri(contentUri);
mDraftMessage = null;
} }
} else if (ContentType.isImageType(contentType) || mDraftMessage =
ContentType.isVCardType(contentType) || sharedText != null ? MessageData.createSharedMessage(sharedText) : null;
ContentType.isAudioType(contentType) || } else if (ContentType.isMediaType(contentType)) {
ContentType.isVideoType(contentType)) {
if (contentUri != null) { if (contentUri != null) {
mDraftMessage = MessageData.createSharedMessage(null); mDraftMessage = MessageData.createSharedMessage(null);
addSharedImagePartToDraft(contentType, contentUri); addSharedPartToDraft(contentType, contentUri);
} else { } else {
mDraftMessage = null; mDraftMessage = null;
} }
} else { } else {
// Unsupported content type. // Unsupported content type.
Assert.fail("Unsupported shared content type for " + contentUri + ": " + contentType LogUtil.e(LogUtil.BUGLE_TAG, "Unsupported shared content type for " + contentUri
+ " (" + intent.getType() + ")"); + ": " + contentType + " (" + intent.getType() + ")");
mDraftMessage = null;
} }
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) { } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
final String contentType = intent.getType(); final String contentType = intent.getType();
if (ContentType.isImageType(contentType)) { // Handle sharing multiple contents.
// Handle sharing multiple images. final ArrayList<Uri> uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
final ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra( if (uris != null && !uris.isEmpty()) {
Intent.EXTRA_STREAM); ArrayMap<Uri, String> uriMap = new ArrayMap<Uri, String>();
if (imageUris != null && imageUris.size() > 0) { StringBuffer strBuffer = new StringBuffer();
mDraftMessage = MessageData.createSharedMessage(null); for (final Uri uri : uris) {
for (final Uri imageUri : imageUris) { if (UriUtil.isFileUri(uri)) {
if (UriUtil.isFileUri(imageUri)) { LogUtil.i(LogUtil.BUGLE_TAG,
LogUtil.i(
LogUtil.BUGLE_TAG,
"Ignoring attachment from file URI which are no longer supported."); "Ignoring attachment from file URI which are no longer supported.");
continue; continue;
} }
final String actualContentType = extractContentType(imageUri, contentType); final String actualContentType = extractContentType(uri, contentType);
addSharedImagePartToDraft(actualContentType, imageUri); 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 {
mDraftMessage = null;
} }
} else { } else {
// Unsupported content type. // No EXTRA_STREAM.
Assert.fail("Unsupported shared content type: " + contentType); LogUtil.e(LogUtil.BUGLE_TAG, "No shared URI.");
mDraftMessage = null;
} }
} else { } else {
// Unsupported action. // Unsupported action.
@@ -141,6 +165,40 @@ public class ShareIntentActivity extends BaseBugleActivity implements
} }
} }
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) { private static String extractContentType(final Uri uri, final String contentType) {
if (uri == null) { if (uri == null) {
return contentType; return contentType;
@@ -171,12 +229,12 @@ public class ShareIntentActivity extends BaseBugleActivity implements
return contentType; return contentType;
} }
private void addSharedImagePartToDraft(final String contentType, final Uri imageUri) { private void addSharedPartToDraft(final String contentType, final Uri uri) {
if (FileUtil.isInPrivateDir(imageUri)) { if (FileUtil.isInPrivateDir(uri)) {
Assert.fail("Cannot send private file " + imageUri.toString()); Assert.fail("Cannot send private file " + uri.toString());
} else { } else {
mDraftMessage.addPart(PendingAttachmentData.createPendingAttachmentData(contentType, mDraftMessage.addPart(
imageUri)); PendingAttachmentData.createPendingAttachmentData(contentType, uri));
} }
} }