MTP file transfers happen in two stages. The SendObjectInfo command sends some information about the file and reserves an ObjectHandle for the new file. The file transfer is then performed using the SendObject command. To support this in the media provider, MtpDatabase.beginSendObject receives the information from SendObjectInfo and creates an row for it in the MTP objects table for the new file. After the file transfer has completed, then MtpDatabase.endSendObject is called. In endSendObject, we run the media scanner on the new file, which will add a row to the images, audio, video or audio playlist table. To avoid the media scanner creating a second row for the file in the MTP objects table, we pass the ObjectHandle created in beginSendObject to the media scanner, which then passes it to the media provider via the content values when it performs its insert. Change-Id: I1ebcc63d6bd4404b0d3a93c703a9d3c097381d3a Signed-off-by: Mike Lockwood <lockwood@android.com>
265 lines
11 KiB
Java
265 lines
11 KiB
Java
/*
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package android.media;
|
|
|
|
import android.content.ContentValues;
|
|
import android.provider.MediaStore.Audio;
|
|
import android.provider.MediaStore.Images;
|
|
import android.provider.MediaStore.Video;
|
|
import android.provider.Mtp;
|
|
import android.media.DecoderCapabilities;
|
|
import android.media.DecoderCapabilities.VideoDecoder;
|
|
import android.media.DecoderCapabilities.AudioDecoder;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* MediaScanner helper class.
|
|
*
|
|
* {@hide}
|
|
*/
|
|
public class MediaFile {
|
|
// comma separated list of all file extensions supported by the media scanner
|
|
public final static String sFileExtensions;
|
|
|
|
// Audio file types
|
|
public static final int FILE_TYPE_MP3 = 1;
|
|
public static final int FILE_TYPE_M4A = 2;
|
|
public static final int FILE_TYPE_WAV = 3;
|
|
public static final int FILE_TYPE_AMR = 4;
|
|
public static final int FILE_TYPE_AWB = 5;
|
|
public static final int FILE_TYPE_WMA = 6;
|
|
public static final int FILE_TYPE_OGG = 7;
|
|
public static final int FILE_TYPE_AAC = 8;
|
|
public static final int FILE_TYPE_MKA = 9;
|
|
private static final int FIRST_AUDIO_FILE_TYPE = FILE_TYPE_MP3;
|
|
private static final int LAST_AUDIO_FILE_TYPE = FILE_TYPE_MKA;
|
|
|
|
// MIDI file types
|
|
public static final int FILE_TYPE_MID = 11;
|
|
public static final int FILE_TYPE_SMF = 12;
|
|
public static final int FILE_TYPE_IMY = 13;
|
|
private static final int FIRST_MIDI_FILE_TYPE = FILE_TYPE_MID;
|
|
private static final int LAST_MIDI_FILE_TYPE = FILE_TYPE_IMY;
|
|
|
|
// Video file types
|
|
public static final int FILE_TYPE_MP4 = 21;
|
|
public static final int FILE_TYPE_M4V = 22;
|
|
public static final int FILE_TYPE_3GPP = 23;
|
|
public static final int FILE_TYPE_3GPP2 = 24;
|
|
public static final int FILE_TYPE_WMV = 25;
|
|
public static final int FILE_TYPE_ASF = 26;
|
|
public static final int FILE_TYPE_MKV = 27;
|
|
public static final int FILE_TYPE_MP2TS = 28;
|
|
private static final int FIRST_VIDEO_FILE_TYPE = FILE_TYPE_MP4;
|
|
private static final int LAST_VIDEO_FILE_TYPE = FILE_TYPE_MP2TS;
|
|
|
|
// Image file types
|
|
public static final int FILE_TYPE_JPEG = 31;
|
|
public static final int FILE_TYPE_GIF = 32;
|
|
public static final int FILE_TYPE_PNG = 33;
|
|
public static final int FILE_TYPE_BMP = 34;
|
|
public static final int FILE_TYPE_WBMP = 35;
|
|
private static final int FIRST_IMAGE_FILE_TYPE = FILE_TYPE_JPEG;
|
|
private static final int LAST_IMAGE_FILE_TYPE = FILE_TYPE_WBMP;
|
|
|
|
// Playlist file types
|
|
public static final int FILE_TYPE_M3U = 41;
|
|
public static final int FILE_TYPE_PLS = 42;
|
|
public static final int FILE_TYPE_WPL = 43;
|
|
private static final int FIRST_PLAYLIST_FILE_TYPE = FILE_TYPE_M3U;
|
|
private static final int LAST_PLAYLIST_FILE_TYPE = FILE_TYPE_WPL;
|
|
|
|
static class MediaFileType {
|
|
|
|
int fileType;
|
|
String mimeType;
|
|
|
|
MediaFileType(int fileType, String mimeType) {
|
|
this.fileType = fileType;
|
|
this.mimeType = mimeType;
|
|
}
|
|
}
|
|
|
|
private static HashMap<String, MediaFileType> sFileTypeMap
|
|
= new HashMap<String, MediaFileType>();
|
|
private static HashMap<String, Integer> sMimeTypeMap
|
|
= new HashMap<String, Integer>();
|
|
// maps file extension to MTP format code
|
|
private static HashMap<String, Integer> sFileTypeToFormatMap
|
|
= new HashMap<String, Integer>();
|
|
// maps mime type to MTP format code
|
|
private static HashMap<String, Integer> sMimeTypeToFormatMap
|
|
= new HashMap<String, Integer>();
|
|
// maps MTP format code to mime type
|
|
private static HashMap<Integer, String> sFormatToMimeTypeMap
|
|
= new HashMap<Integer, String>();
|
|
|
|
static void addFileType(String extension, int fileType, String mimeType) {
|
|
sFileTypeMap.put(extension, new MediaFileType(fileType, mimeType));
|
|
sMimeTypeMap.put(mimeType, Integer.valueOf(fileType));
|
|
}
|
|
|
|
static void addFileType(String extension, int fileType, String mimeType, int mtpFormatCode) {
|
|
addFileType(extension, fileType, mimeType);
|
|
sFileTypeToFormatMap.put(extension, Integer.valueOf(mtpFormatCode));
|
|
sMimeTypeToFormatMap.put(mimeType, Integer.valueOf(mtpFormatCode));
|
|
sFormatToMimeTypeMap.put(mtpFormatCode, mimeType);
|
|
}
|
|
|
|
private static boolean isWMAEnabled() {
|
|
List<AudioDecoder> decoders = DecoderCapabilities.getAudioDecoders();
|
|
for (AudioDecoder decoder: decoders) {
|
|
if (decoder == AudioDecoder.AUDIO_DECODER_WMA) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private static boolean isWMVEnabled() {
|
|
List<VideoDecoder> decoders = DecoderCapabilities.getVideoDecoders();
|
|
for (VideoDecoder decoder: decoders) {
|
|
if (decoder == VideoDecoder.VIDEO_DECODER_WMV) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static {
|
|
addFileType("MP3", FILE_TYPE_MP3, "audio/mpeg", Mtp.Object.FORMAT_MP3);
|
|
addFileType("M4A", FILE_TYPE_M4A, "audio/mp4", Mtp.Object.FORMAT_MPEG);
|
|
addFileType("WAV", FILE_TYPE_WAV, "audio/x-wav", Mtp.Object.FORMAT_WAV);
|
|
addFileType("AMR", FILE_TYPE_AMR, "audio/amr");
|
|
addFileType("AWB", FILE_TYPE_AWB, "audio/amr-wb");
|
|
if (isWMAEnabled()) {
|
|
addFileType("WMA", FILE_TYPE_WMA, "audio/x-ms-wma", Mtp.Object.FORMAT_WMA);
|
|
}
|
|
addFileType("OGG", FILE_TYPE_OGG, "application/ogg", Mtp.Object.FORMAT_OGG);
|
|
addFileType("OGA", FILE_TYPE_OGG, "application/ogg", Mtp.Object.FORMAT_OGG);
|
|
addFileType("AAC", FILE_TYPE_AAC, "audio/aac", Mtp.Object.FORMAT_AAC);
|
|
addFileType("MKA", FILE_TYPE_MKA, "audio/x-matroska");
|
|
|
|
addFileType("MID", FILE_TYPE_MID, "audio/midi");
|
|
addFileType("MIDI", FILE_TYPE_MID, "audio/midi");
|
|
addFileType("XMF", FILE_TYPE_MID, "audio/midi");
|
|
addFileType("RTTTL", FILE_TYPE_MID, "audio/midi");
|
|
addFileType("SMF", FILE_TYPE_SMF, "audio/sp-midi");
|
|
addFileType("IMY", FILE_TYPE_IMY, "audio/imelody");
|
|
addFileType("RTX", FILE_TYPE_MID, "audio/midi");
|
|
addFileType("OTA", FILE_TYPE_MID, "audio/midi");
|
|
|
|
addFileType("MPEG", FILE_TYPE_MP4, "video/mpeg", Mtp.Object.FORMAT_MPEG);
|
|
addFileType("MP4", FILE_TYPE_MP4, "video/mp4", Mtp.Object.FORMAT_MPEG);
|
|
addFileType("M4V", FILE_TYPE_M4V, "video/mp4", Mtp.Object.FORMAT_MPEG);
|
|
addFileType("3GP", FILE_TYPE_3GPP, "video/3gpp", Mtp.Object.FORMAT_3GP_CONTAINER);
|
|
addFileType("3GPP", FILE_TYPE_3GPP, "video/3gpp", Mtp.Object.FORMAT_3GP_CONTAINER);
|
|
addFileType("3G2", FILE_TYPE_3GPP2, "video/3gpp2", Mtp.Object.FORMAT_3GP_CONTAINER);
|
|
addFileType("3GPP2", FILE_TYPE_3GPP2, "video/3gpp2", Mtp.Object.FORMAT_3GP_CONTAINER);
|
|
addFileType("MKV", FILE_TYPE_MKV, "video/x-matroska");
|
|
addFileType("WEBM", FILE_TYPE_MKV, "video/x-matroska");
|
|
addFileType("TS", FILE_TYPE_MP2TS, "video/mp2ts");
|
|
|
|
if (isWMVEnabled()) {
|
|
addFileType("WMV", FILE_TYPE_WMV, "video/x-ms-wmv", Mtp.Object.FORMAT_WMV);
|
|
addFileType("ASF", FILE_TYPE_ASF, "video/x-ms-asf");
|
|
}
|
|
|
|
addFileType("JPG", FILE_TYPE_JPEG, "image/jpeg", Mtp.Object.FORMAT_EXIF_JPEG);
|
|
addFileType("JPEG", FILE_TYPE_JPEG, "image/jpeg", Mtp.Object.FORMAT_EXIF_JPEG);
|
|
addFileType("GIF", FILE_TYPE_GIF, "image/gif", Mtp.Object.FORMAT_GIF);
|
|
addFileType("PNG", FILE_TYPE_PNG, "image/png", Mtp.Object.FORMAT_PNG);
|
|
addFileType("BMP", FILE_TYPE_BMP, "image/x-ms-bmp", Mtp.Object.FORMAT_BMP);
|
|
addFileType("WBMP", FILE_TYPE_WBMP, "image/vnd.wap.wbmp");
|
|
|
|
addFileType("M3U", FILE_TYPE_M3U, "audio/x-mpegurl", Mtp.Object.FORMAT_M3U_PLAYLIST);
|
|
addFileType("PLS", FILE_TYPE_PLS, "audio/x-scpls", Mtp.Object.FORMAT_PLS_PLAYLIST);
|
|
addFileType("WPL", FILE_TYPE_WPL, "application/vnd.ms-wpl", Mtp.Object.FORMAT_WPL_PLAYLIST);
|
|
|
|
// compute file extensions list for native Media Scanner
|
|
StringBuilder builder = new StringBuilder();
|
|
Iterator<String> iterator = sFileTypeMap.keySet().iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
if (builder.length() > 0) {
|
|
builder.append(',');
|
|
}
|
|
builder.append(iterator.next());
|
|
}
|
|
sFileExtensions = builder.toString();
|
|
}
|
|
|
|
public static boolean isAudioFileType(int fileType) {
|
|
return ((fileType >= FIRST_AUDIO_FILE_TYPE &&
|
|
fileType <= LAST_AUDIO_FILE_TYPE) ||
|
|
(fileType >= FIRST_MIDI_FILE_TYPE &&
|
|
fileType <= LAST_MIDI_FILE_TYPE));
|
|
}
|
|
|
|
public static boolean isVideoFileType(int fileType) {
|
|
return (fileType >= FIRST_VIDEO_FILE_TYPE &&
|
|
fileType <= LAST_VIDEO_FILE_TYPE);
|
|
}
|
|
|
|
public static boolean isImageFileType(int fileType) {
|
|
return (fileType >= FIRST_IMAGE_FILE_TYPE &&
|
|
fileType <= LAST_IMAGE_FILE_TYPE);
|
|
}
|
|
|
|
public static boolean isPlayListFileType(int fileType) {
|
|
return (fileType >= FIRST_PLAYLIST_FILE_TYPE &&
|
|
fileType <= LAST_PLAYLIST_FILE_TYPE);
|
|
}
|
|
|
|
public static MediaFileType getFileType(String path) {
|
|
int lastDot = path.lastIndexOf(".");
|
|
if (lastDot < 0)
|
|
return null;
|
|
return sFileTypeMap.get(path.substring(lastDot + 1).toUpperCase());
|
|
}
|
|
|
|
public static int getFileTypeForMimeType(String mimeType) {
|
|
Integer value = sMimeTypeMap.get(mimeType);
|
|
return (value == null ? 0 : value.intValue());
|
|
}
|
|
|
|
public static int getFormatCode(String fileName, String mimeType) {
|
|
if (mimeType != null) {
|
|
Integer value = sMimeTypeToFormatMap.get(mimeType);
|
|
if (value != null) {
|
|
return value.intValue();
|
|
}
|
|
}
|
|
int lastDot = fileName.lastIndexOf('.');
|
|
if (lastDot > 0) {
|
|
String extension = fileName.substring(lastDot + 1);
|
|
Integer value = sFileTypeToFormatMap.get(extension);
|
|
if (value != null) {
|
|
return value.intValue();
|
|
}
|
|
}
|
|
return Mtp.Object.FORMAT_UNDEFINED;
|
|
}
|
|
|
|
public static String getMimeTypeForFormatCode(int formatCode) {
|
|
return sFormatToMimeTypeMap.get(formatCode);
|
|
}
|
|
}
|