~xdavidwu/saf-cephfs

9d19e64011a1fd4582c12ed2f206b579a86dd412 — xdavidwu 2 years ago 4b8fc7c
MediaMetadataReader: support audio
M src/main/java/org/safcephfs/CephFSDocumentsProvider.java => src/main/java/org/safcephfs/CephFSDocumentsProvider.java +2 -1
@@ 415,7 415,8 @@ public class CephFSDocumentsProvider extends DocumentsProvider {
			ParcelFileDescriptor fd = openDocument(documentId, "r", null);

			Bundle metadata = new Bundle();
			MediaMetadataReader.getMetadata(metadata, fd.getFileDescriptor());
			MediaMetadataReader.getMetadata(metadata, fd.getFileDescriptor(),
				mimeType);
			try {
				fd.close();
			} catch (IOException e) {

M src/main/java/org/safcephfs/MediaMetadataReader.java => src/main/java/org/safcephfs/MediaMetadataReader.java +42 -12
@@ 11,37 11,67 @@ import java.util.HashMap;
import java.util.Map;

public final class MediaMetadataReader {
	private static final Map<Integer, String> NAME_MAPPING = new HashMap<>();
	private static final Map<Integer, String> NAME_MAPPING_VIDEO = new HashMap<>();
	private static final Map<Integer, String> NAME_MAPPING_AUDIO = new HashMap<>();

	private static final int TYPE_INT = 0;

	private static final Map<Integer, Integer> TYPE_MAPPING = new HashMap<>();

	static {
		NAME_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH, ExifInterface.TAG_IMAGE_WIDTH);
		NAME_MAPPING_VIDEO.put(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH, ExifInterface.TAG_IMAGE_WIDTH);
		TYPE_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH, TYPE_INT);

		NAME_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT, ExifInterface.TAG_IMAGE_LENGTH);
		NAME_MAPPING_VIDEO.put(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT, ExifInterface.TAG_IMAGE_LENGTH);
		TYPE_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT, TYPE_INT);

		// in ms
		NAME_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_DURATION, MediaMetadata.METADATA_KEY_DURATION);
		NAME_MAPPING_VIDEO.put(MediaMetadataRetriever.METADATA_KEY_DURATION, MediaMetadata.METADATA_KEY_DURATION);
		NAME_MAPPING_AUDIO.put(MediaMetadataRetriever.METADATA_KEY_DURATION, MediaMetadata.METADATA_KEY_DURATION);
		TYPE_MAPPING.put(MediaMetadataRetriever.METADATA_KEY_DURATION, TYPE_INT);

		NAME_MAPPING_AUDIO.put(MediaMetadataRetriever.METADATA_KEY_ARTIST, MediaMetadata.METADATA_KEY_ARTIST);

		NAME_MAPPING_AUDIO.put(MediaMetadataRetriever.METADATA_KEY_COMPOSER, MediaMetadata.METADATA_KEY_COMPOSER);

		NAME_MAPPING_AUDIO.put(MediaMetadataRetriever.METADATA_KEY_ALBUM, MediaMetadata.METADATA_KEY_ALBUM);
	}

	public static final String METADATA_KEY_VIDEO = "android.media.metadata.video";
	public static final String METADATA_KEY_AUDIO = "android.media.metadata.audio";

	// Not sure what formats MediaMetadataRetriever supports, let's try all videos
	public static boolean isSupportedMimeType(String mimeType) {
		return isSupportedVideoMimeType(mimeType) ||
			isSupportedAudioMimeType(mimeType);
	}

	// Not sure what formats MediaMetadataRetriever supports, let's try all
	private static boolean isSupportedVideoMimeType(String mimeType) {
		return mimeType.startsWith("video/");
	}

	public static void getMetadata(Bundle metadata, FileDescriptor fd) {
		Bundle videoMetadata = new Bundle();
	private static boolean isSupportedAudioMimeType(String mimeType) {
		return mimeType.startsWith("audio/");
	}

	public static void getMetadata(Bundle metadata, FileDescriptor fd,
			String mimeType) {
		String metadataType;
		Map<Integer, String> nameMapping;
		if (isSupportedVideoMimeType(mimeType)) {
			metadataType = METADATA_KEY_VIDEO;
			nameMapping = NAME_MAPPING_VIDEO;
		} else if (isSupportedAudioMimeType(mimeType)) {
			metadataType = METADATA_KEY_AUDIO;
			nameMapping = NAME_MAPPING_AUDIO;
		} else {
			return;
		}
		Bundle typeSpecificMetadata = new Bundle();
		MediaMetadataRetriever retriever = new MediaMetadataRetriever();
		retriever.setDataSource(fd);

		for (int key: NAME_MAPPING.keySet()) {
		for (int key: nameMapping.keySet()) {
			String raw = retriever.extractMetadata(key);
			if (raw == null) {
				continue;


@@ 49,17 79,17 @@ public final class MediaMetadataReader {

			Integer type = TYPE_MAPPING.get(key);
			if (type == null) {
				videoMetadata.putString(NAME_MAPPING.get(key), raw);
				typeSpecificMetadata.putString(nameMapping.get(key), raw);
			} else {
				switch (type) {
				case TYPE_INT:
					videoMetadata.putInt(NAME_MAPPING.get(key), Integer.parseInt(raw));
					typeSpecificMetadata.putInt(nameMapping.get(key), Integer.parseInt(raw));
					break;
				}
			}
		}
		metadata.putBundle(METADATA_KEY_VIDEO, videoMetadata);
		String[] types = {METADATA_KEY_VIDEO};
		metadata.putBundle(metadataType, typeSpecificMetadata);
		String[] types = {metadataType};
		metadata.putStringArray(DocumentsContract.METADATA_TYPES, types);
	}
}