Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Gutenberg] - Filter media types when sharing files to the app #19754

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading