Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bb058358 authored by Shai Barack's avatar Shai Barack
Browse files

Make sanitizeMediaMetadata optimization more robust

Even if the MediaMetadata contains URIs, but they don't require sanitization,
avoid making a copy of the MediaMetadata.
This is a follow-up to ag/32909078 that makes the optimization apply in
additional use cases.

Flag: EXEMPT bugfix
Bug: 407234313
Bug: 271851153
Change-Id: I79ea867d8ad84109ed4483bd413ea32cf1aecd8a
parent 5df7e57f
Loading
Loading
Loading
Loading
+14 −14
Original line number Diff line number Diff line
@@ -1266,19 +1266,8 @@ public class MediaSessionRecord extends MediaSessionRecordImpl implements IBinde
                return null;
            }

            // Check if there are URIs to sanitize
            boolean hasUris = false;
            for (String key : ART_URIS) {
                if (metadata.containsKey(key)) {
                    hasUris = true;
                    break;
                }
            }
            if (!hasUris) {
                return metadata;
            }

            MediaMetadata.Builder metadataBuilder = new MediaMetadata.Builder(metadata);
            // Find all URIs that need to be sanitized
            List<String> urisToSanitize = new ArrayList<>();
            for (String key : ART_URIS) {
                String uriString = metadata.getString(key);
                if (TextUtils.isEmpty(uriString)) {
@@ -1295,8 +1284,19 @@ public class MediaSessionRecord extends MediaSessionRecordImpl implements IBinde
                            Intent.FLAG_GRANT_READ_URI_PERMISSION,
                            ContentProvider.getUserIdFromUri(uri, getUserId()));
                } catch (SecurityException e) {
                    metadataBuilder.putString(key, null);
                    urisToSanitize.add(key);
                }
            }

            // Avoid creating a Builder (and copying the metadata) if there are no URIs to
            // sanitize.
            if (urisToSanitize.isEmpty()) {
                return metadata;
            }

            MediaMetadata.Builder metadataBuilder = new MediaMetadata.Builder(metadata);
            for (String key : urisToSanitize) {
                metadataBuilder.putString(key, null);
            }
            MediaMetadata sanitizedMetadata = metadataBuilder.build();
            // sanitizedMetadata.size() guarantees that the underlying bundle is unparceled