Handle unsupported or empty MIME type

Apps sometimes use unsupported MIME type or empty/null MIME type in
MediaStore insert operation. When we get invalid MIME type, we try to
guess the MIME type from other values like file extension in
DISPLAY_NAME or from content uri. Previously, When apps used unsupported
MIME type and no file extension, we allowed them to insert the file if
primary MIME type matched the default MIME type of the content uri.
For example:
* App inserts (DISPLAY_NAME=Foo, MIME_TYPE=image/*) using
MediaStore.Images uri. We allow this insert because primary MIME type
(i.e., "image") matches the default MIME type of the content uri.
* When Foo is scanned, MIME type is extracted from the file extension,
and Foo's MIME type will be updated to non-media type.

This leads to issues like:
(1) Apps holding on to content uri will not be able to access newly
inserted file using the same uri after scanning the file.
(2) This behavior can be easily misused to create a non-media file in
media directory.

To avoid these issues, in this CL, we try to restrict apps from using
unsupported MIME type when MIME type from the file extension doesn't
match default MIME type of the content uri. For apps targeting Q or
lower, we try to use default MIME type of the content uri instead of
throwing error.

Bug: 157127219
Test: atest MediaProviderTest#testBuildData_InvalidSecondaryTypes
Test: atest MediaProviderTest#testBuildData_EmptyTypes
Test: atest MediaProviderTest#testEnsureFileColumns_InvalidMimeType_targetSdkQ
Test: atest --test-mapping /packages/providers/MediaProvider
Change-Id: I3a01845a49dc654235383450939b39cd8b95097f
2 files changed