Skip to content

Commit

Permalink
Merge pull request #19754 from wordpress-mobile/gutenberg/filter-medi…
Browse files Browse the repository at this point in the history
…a-sharing

[Gutenberg] - Filter media types when sharing files to the app
  • Loading branch information
Gerardo Pacheco authored Dec 12, 2023
2 parents e7e27a3 + 9db5727 commit 3c7e9ce
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

24.0
-----

* [*] Filter media types when sharing files to the editor [https://github.com/wordpress-mobile/WordPress-Android/pull/19754]

23.9
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,23 @@ private void downloadExternalMedia() {
if (Intent.ACTION_SEND_MULTIPLE.equals(getIntent().getAction())) {
ArrayList<Uri> externalUris = getIntent().getParcelableArrayListExtra((Intent.EXTRA_STREAM));
for (Uri uri : externalUris) {
mLocalMediaUris.add(MediaUtils.downloadExternalMedia(this, uri));
if (isAllowedMediaType(uri)) {
mLocalMediaUris.add(MediaUtils.downloadExternalMedia(this, uri));
}
}
} else if (Intent.ACTION_SEND.equals(getIntent().getAction())) {
Uri externalUri = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
mLocalMediaUris.add(MediaUtils.downloadExternalMedia(this, externalUri));
if (isAllowedMediaType(externalUri)) {
mLocalMediaUris.add(MediaUtils.downloadExternalMedia(this, externalUri));
}
}
}

private boolean isAllowedMediaType(@NonNull Uri uri) {
String filePath = MediaUtils.getRealPathFromURI(this, uri);
return MediaUtils.isValidImage(filePath) || MediaUtils.isVideo(filePath);
}

private void initShareFragment() {
ShareIntentReceiverFragment shareIntentReceiverFragment = ShareIntentReceiverFragment
.newInstance(!isSharingText(), loadLastUsedBlogLocalId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ private void newPostSetup() {

private void newPostFromShareAction() {
Intent intent = getIntent();
if (isMediaTypeIntent(intent)) {
if (isMediaTypeIntent(intent, null)) {
newPostSetup();
setPostMediaFromShareAction();
} else {
Expand Down Expand Up @@ -2695,30 +2695,44 @@ private void setPostMediaFromShareAction() {
// Check for shared media
if (intent.hasExtra(Intent.EXTRA_STREAM)) {
String action = intent.getAction();
ArrayList<Uri> sharedUris;
ArrayList<Uri> sharedUris = new ArrayList<>();

if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
sharedUris = intent.getParcelableArrayListExtra((Intent.EXTRA_STREAM));
ArrayList<Uri> potentialUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (potentialUris != null) {
for (Uri uri : potentialUris) {
if (isMediaTypeIntent(intent, uri)) {
sharedUris.add(uri);
}
}
}
} else {
// For a single media share, we only allow images and video types
if (isMediaTypeIntent(intent)) {
sharedUris = new ArrayList<>();
if (isMediaTypeIntent(intent, null)) {
sharedUris.add(intent.getParcelableExtra(Intent.EXTRA_STREAM));
} else {
sharedUris = null;
}
}

if (sharedUris != null) {
if (!sharedUris.isEmpty()) {
// removing this from the intent so it doesn't insert the media items again on each Activity re-creation
getIntent().removeExtra(Intent.EXTRA_STREAM);

mEditorMedia.addNewMediaItemsToEditorAsync(sharedUris, false);
}
}
}

private boolean isMediaTypeIntent(Intent intent) {
String type = intent.getType();
private boolean isMediaTypeIntent(@NonNull Intent intent, @Nullable Uri uri) {
String type = null;

if (uri != null) {
String extension = MimeTypeMap.getFileExtensionFromUrl(String.valueOf(uri));
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
} else {
type = intent.getType();
}
return type != null && (type.startsWith("image") || type.startsWith("video"));
}

Expand Down

0 comments on commit 3c7e9ce

Please sign in to comment.