DO NOT MERGE MediaScanner: reimplement the ".nomedia" feature for hiding files from the media provider

Previously we ignored any files and directories that had name started with '.'
and ignored any directories that contained a ".nomedia" file.
Now to support transferring any file via MTP, we now add these previously ignored files
to the media database, but will not mark them as audio, video, image or playlist files.
That way they will be included in the files table but will be hidden from the
audio, video, images and playlist views that are used by apps like Music and Gallery.

Bug: 3405327

Change-Id: Ib707f329be98878501952b6054998518a6eabbbd
Signed-off-by: Mike Lockwood <lockwood@android.com>
This commit is contained in:
Mike Lockwood
2011-04-24 11:15:09 -07:00
parent 7dd592298a
commit fb6232635d
5 changed files with 123 additions and 123 deletions

View File

@ -55,7 +55,7 @@ private:
status_t doProcessDirectory( status_t doProcessDirectory(
char *path, int pathRemaining, MediaScannerClient &client, char *path, int pathRemaining, MediaScannerClient &client,
ExceptionCheck exceptionCheck, void *exceptionEnv); bool noMedia, ExceptionCheck exceptionCheck, void *exceptionEnv);
MediaScanner(const MediaScanner &); MediaScanner(const MediaScanner &);
MediaScanner &operator=(const MediaScanner &); MediaScanner &operator=(const MediaScanner &);
@ -72,10 +72,9 @@ public:
void endFile(); void endFile();
virtual bool scanFile(const char* path, long long lastModified, virtual bool scanFile(const char* path, long long lastModified,
long long fileSize, bool isDirectory) = 0; long long fileSize, bool isDirectory, bool noMedia) = 0;
virtual bool handleStringTag(const char* name, const char* value) = 0; virtual bool handleStringTag(const char* name, const char* value) = 0;
virtual bool setMimeType(const char* mimeType) = 0; virtual bool setMimeType(const char* mimeType) = 0;
virtual bool addNoMediaFolder(const char* path) = 0;
protected: protected:
void convertValues(uint32_t encoding); void convertValues(uint32_t encoding);

View File

@ -423,9 +423,10 @@ public class MediaScanner
private long mFileSize; private long mFileSize;
private String mWriter; private String mWriter;
private int mCompilation; private int mCompilation;
private boolean mNoMedia; // flag to suppress file from appearing in media tables
public FileCacheEntry beginFile(String path, String mimeType, long lastModified, public FileCacheEntry beginFile(String path, String mimeType, long lastModified,
long fileSize, boolean isDirectory) { long fileSize, boolean isDirectory, boolean noMedia) {
mMimeType = mimeType; mMimeType = mimeType;
mFileType = 0; mFileType = 0;
mFileSize = fileSize; mFileSize = fileSize;
@ -436,28 +437,31 @@ public class MediaScanner
// to avoid memory allocation // to avoid memory allocation
int lastSlash = path.lastIndexOf('/'); int lastSlash = path.lastIndexOf('/');
if (lastSlash >= 0 && lastSlash + 2 < path.length()) { if (lastSlash >= 0 && lastSlash + 2 < path.length()) {
// ignore those ._* files created by MacOS if (!noMedia) {
if (path.regionMatches(lastSlash + 1, "._", 0, 2)) { // ignore those ._* files created by MacOS
return null; if (path.regionMatches(lastSlash + 1, "._", 0, 2)) {
} noMedia = true;
// ignore album art files created by Windows Media Player:
// Folder.jpg, AlbumArtSmall.jpg, AlbumArt_{...}_Large.jpg
// and AlbumArt_{...}_Small.jpg
if (path.regionMatches(true, path.length() - 4, ".jpg", 0, 4)) {
if (path.regionMatches(true, lastSlash + 1, "AlbumArt_{", 0, 10) ||
path.regionMatches(true, lastSlash + 1, "AlbumArt.", 0, 9)) {
return null;
} }
int length = path.length() - lastSlash - 1;
if ((length == 17 && path.regionMatches( // ignore album art files created by Windows Media Player:
true, lastSlash + 1, "AlbumArtSmall", 0, 13)) || // Folder.jpg, AlbumArtSmall.jpg, AlbumArt_{...}_Large.jpg
(length == 10 // and AlbumArt_{...}_Small.jpg
&& path.regionMatches(true, lastSlash + 1, "Folder", 0, 6))) { if (path.regionMatches(true, path.length() - 4, ".jpg", 0, 4)) {
return null; if (path.regionMatches(true, lastSlash + 1, "AlbumArt_{", 0, 10) ||
path.regionMatches(true, lastSlash + 1, "AlbumArt.", 0, 9)) {
noMedia = true;
}
int length = path.length() - lastSlash - 1;
if ((length == 17 && path.regionMatches(
true, lastSlash + 1, "AlbumArtSmall", 0, 13)) ||
(length == 10
&& path.regionMatches(true, lastSlash + 1, "Folder", 0, 6))) {
noMedia = true;
}
} }
} }
} }
mNoMedia = noMedia;
// try mimeType first, if it is specified // try mimeType first, if it is specified
if (mimeType != null) { if (mimeType != null) {
@ -524,36 +528,41 @@ public class MediaScanner
return entry; return entry;
} }
public void scanFile(String path, long lastModified, long fileSize, boolean isDirectory) { public void scanFile(String path, long lastModified, long fileSize,
boolean isDirectory, boolean noMedia) {
// This is the callback funtion from native codes. // This is the callback funtion from native codes.
// Log.v(TAG, "scanFile: "+path); // Log.v(TAG, "scanFile: "+path);
doScanFile(path, null, lastModified, fileSize, isDirectory, false); doScanFile(path, null, lastModified, fileSize, isDirectory, false, noMedia);
} }
public Uri doScanFile(String path, String mimeType, long lastModified, public Uri doScanFile(String path, String mimeType, long lastModified,
long fileSize, boolean isDirectory, boolean scanAlways) { long fileSize, boolean isDirectory, boolean scanAlways, boolean noMedia) {
Uri result = null; Uri result = null;
// long t1 = System.currentTimeMillis(); // long t1 = System.currentTimeMillis();
try { try {
FileCacheEntry entry = beginFile(path, mimeType, lastModified, FileCacheEntry entry = beginFile(path, mimeType, lastModified,
fileSize, isDirectory); fileSize, isDirectory, noMedia);
// rescan for metadata if file was modified since last scan // rescan for metadata if file was modified since last scan
if (entry != null && (entry.mLastModifiedChanged || scanAlways)) { if (entry != null && (entry.mLastModifiedChanged || scanAlways)) {
String lowpath = path.toLowerCase(); if (noMedia) {
boolean ringtones = (lowpath.indexOf(RINGTONES_DIR) > 0); result = endFile(entry, false, false, false, false, false);
boolean notifications = (lowpath.indexOf(NOTIFICATIONS_DIR) > 0); } else {
boolean alarms = (lowpath.indexOf(ALARMS_DIR) > 0); String lowpath = path.toLowerCase();
boolean podcasts = (lowpath.indexOf(PODCAST_DIR) > 0); boolean ringtones = (lowpath.indexOf(RINGTONES_DIR) > 0);
boolean music = (lowpath.indexOf(MUSIC_DIR) > 0) || boolean notifications = (lowpath.indexOf(NOTIFICATIONS_DIR) > 0);
(!ringtones && !notifications && !alarms && !podcasts); boolean alarms = (lowpath.indexOf(ALARMS_DIR) > 0);
boolean podcasts = (lowpath.indexOf(PODCAST_DIR) > 0);
boolean music = (lowpath.indexOf(MUSIC_DIR) > 0) ||
(!ringtones && !notifications && !alarms && !podcasts);
// we only extract metadata for audio and video files // we only extract metadata for audio and video files
if (MediaFile.isAudioFileType(mFileType) if (MediaFile.isAudioFileType(mFileType)
|| MediaFile.isVideoFileType(mFileType)) { || MediaFile.isVideoFileType(mFileType)) {
processFile(path, mimeType, this); processFile(path, mimeType, this);
}
result = endFile(entry, ringtones, notifications, alarms, music, podcasts);
} }
result = endFile(entry, ringtones, notifications, alarms, music, podcasts);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "RemoteException in MediaScanner.scanFile()", e); Log.e(TAG, "RemoteException in MediaScanner.scanFile()", e);
@ -690,27 +699,31 @@ public class MediaScanner
map.put(MediaStore.MediaColumns.SIZE, mFileSize); map.put(MediaStore.MediaColumns.SIZE, mFileSize);
map.put(MediaStore.MediaColumns.MIME_TYPE, mMimeType); map.put(MediaStore.MediaColumns.MIME_TYPE, mMimeType);
if (MediaFile.isVideoFileType(mFileType)) { if (!mNoMedia) {
map.put(Video.Media.ARTIST, (mArtist != null && mArtist.length() > 0 ? mArtist : MediaStore.UNKNOWN_STRING)); if (MediaFile.isVideoFileType(mFileType)) {
map.put(Video.Media.ALBUM, (mAlbum != null && mAlbum.length() > 0 ? mAlbum : MediaStore.UNKNOWN_STRING)); map.put(Video.Media.ARTIST, (mArtist != null && mArtist.length() > 0
map.put(Video.Media.DURATION, mDuration); ? mArtist : MediaStore.UNKNOWN_STRING));
// FIXME - add RESOLUTION map.put(Video.Media.ALBUM, (mAlbum != null && mAlbum.length() > 0
} else if (MediaFile.isImageFileType(mFileType)) { ? mAlbum : MediaStore.UNKNOWN_STRING));
// FIXME - add DESCRIPTION map.put(Video.Media.DURATION, mDuration);
} else if (MediaFile.isAudioFileType(mFileType)) { // FIXME - add RESOLUTION
map.put(Audio.Media.ARTIST, (mArtist != null && mArtist.length() > 0) ? } else if (MediaFile.isImageFileType(mFileType)) {
mArtist : MediaStore.UNKNOWN_STRING); // FIXME - add DESCRIPTION
map.put(Audio.Media.ALBUM_ARTIST, (mAlbumArtist != null && } else if (MediaFile.isAudioFileType(mFileType)) {
mAlbumArtist.length() > 0) ? mAlbumArtist : null); map.put(Audio.Media.ARTIST, (mArtist != null && mArtist.length() > 0) ?
map.put(Audio.Media.ALBUM, (mAlbum != null && mAlbum.length() > 0) ? mArtist : MediaStore.UNKNOWN_STRING);
mAlbum : MediaStore.UNKNOWN_STRING); map.put(Audio.Media.ALBUM_ARTIST, (mAlbumArtist != null &&
map.put(Audio.Media.COMPOSER, mComposer); mAlbumArtist.length() > 0) ? mAlbumArtist : null);
if (mYear != 0) { map.put(Audio.Media.ALBUM, (mAlbum != null && mAlbum.length() > 0) ?
map.put(Audio.Media.YEAR, mYear); mAlbum : MediaStore.UNKNOWN_STRING);
map.put(Audio.Media.COMPOSER, mComposer);
if (mYear != 0) {
map.put(Audio.Media.YEAR, mYear);
}
map.put(Audio.Media.TRACK, mTrack);
map.put(Audio.Media.DURATION, mDuration);
map.put(Audio.Media.COMPILATION, mCompilation);
} }
map.put(Audio.Media.TRACK, mTrack);
map.put(Audio.Media.DURATION, mDuration);
map.put(Audio.Media.COMPILATION, mCompilation);
} }
return map; return map;
} }
@ -720,7 +733,7 @@ public class MediaScanner
throws RemoteException { throws RemoteException {
// update database // update database
// use album artist if artist is missing // use album artist if artist is missing
if (mArtist == null || mArtist.length() == 0) { if (mArtist == null || mArtist.length() == 0) {
mArtist = mAlbumArtist; mArtist = mAlbumArtist;
} }
@ -762,7 +775,7 @@ public class MediaScanner
values.put(Audio.Media.IS_ALARM, alarms); values.put(Audio.Media.IS_ALARM, alarms);
values.put(Audio.Media.IS_MUSIC, music); values.put(Audio.Media.IS_MUSIC, music);
values.put(Audio.Media.IS_PODCAST, podcasts); values.put(Audio.Media.IS_PODCAST, podcasts);
} else if (mFileType == MediaFile.FILE_TYPE_JPEG) { } else if (mFileType == MediaFile.FILE_TYPE_JPEG && !mNoMedia) {
ExifInterface exif = null; ExifInterface exif = null;
try { try {
exif = new ExifInterface(entry.mPath); exif = new ExifInterface(entry.mPath);
@ -815,12 +828,14 @@ public class MediaScanner
} }
Uri tableUri = mFilesUri; Uri tableUri = mFilesUri;
if (MediaFile.isVideoFileType(mFileType)) { if (!mNoMedia) {
tableUri = mVideoUri; if (MediaFile.isVideoFileType(mFileType)) {
} else if (MediaFile.isImageFileType(mFileType)) { tableUri = mVideoUri;
tableUri = mImagesUri; } else if (MediaFile.isImageFileType(mFileType)) {
} else if (MediaFile.isAudioFileType(mFileType)) { tableUri = mImagesUri;
tableUri = mAudioUri; } else if (MediaFile.isAudioFileType(mFileType)) {
tableUri = mAudioUri;
}
} }
Uri result = null; Uri result = null;
if (rowId == 0) { if (rowId == 0) {
@ -931,25 +946,6 @@ public class MediaScanner
} }
} }
public void addNoMediaFolder(String path) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.ImageColumns.DATA, "");
String [] pathSpec = new String[] {path + '%'};
try {
// These tables have DELETE_FILE triggers that delete the file from the
// sd card when deleting the database entry. We don't want to do this in
// this case, since it would cause those files to be removed if a .nomedia
// file was added after the fact, when in that case we only want the database
// entries to be removed.
mMediaProvider.update(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values,
MediaStore.Images.ImageColumns.DATA + " LIKE ?", pathSpec);
mMediaProvider.update(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values,
MediaStore.Images.ImageColumns.DATA + " LIKE ?", pathSpec);
} catch (RemoteException e) {
throw new RuntimeException();
}
}
private int getFileTypeFromDrm(String path) { private int getFileTypeFromDrm(String path) {
if (!isDrmEnabled()) { if (!isDrmEnabled()) {
return 0; return 0;
@ -1230,13 +1226,37 @@ public class MediaScanner
// always scan the file, so we can return the content://media Uri for existing files // always scan the file, so we can return the content://media Uri for existing files
return mClient.doScanFile(path, mimeType, lastModifiedSeconds, file.length(), return mClient.doScanFile(path, mimeType, lastModifiedSeconds, file.length(),
false, true); false, true, false);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "RemoteException in MediaScanner.scanFile()", e); Log.e(TAG, "RemoteException in MediaScanner.scanFile()", e);
return null; return null;
} }
} }
public static boolean isNoMediaPath(String path) {
if (path == null) return false;
// return true if file or any parent directory has name starting with a dot
if (path.indexOf("/.") >= 0) return true;
// now check to see if any parent directories have a ".nomedia" file
// start from 1 so we don't bother checking in the root directory
int offset = 1;
while (offset >= 0) {
int slashIndex = path.indexOf('/', offset);
if (slashIndex > offset) {
slashIndex++; // move past slash
File file = new File(path.substring(0, slashIndex) + ".nomedia");
if (file.exists()) {
// we have a .nomedia in one of the parent directories
return true;
}
}
offset = slashIndex;
}
return false;
}
public void scanMtpFile(String path, String volumeName, int objectHandle, int format) { public void scanMtpFile(String path, String volumeName, int objectHandle, int format) {
initialize(volumeName); initialize(volumeName);
MediaFile.MediaFileType mediaFileType = MediaFile.getFileType(path); MediaFile.MediaFileType mediaFileType = MediaFile.getFileType(path);
@ -1281,7 +1301,7 @@ public class MediaScanner
// always scan the file, so we can return the content://media Uri for existing files // always scan the file, so we can return the content://media Uri for existing files
mClient.doScanFile(path, mediaFileType.mimeType, lastModifiedSeconds, file.length(), mClient.doScanFile(path, mediaFileType.mimeType, lastModifiedSeconds, file.length(),
(format == MtpConstants.FORMAT_ASSOCIATION), true); (format == MtpConstants.FORMAT_ASSOCIATION), true, isNoMediaPath(path));
} }
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "RemoteException in MediaScanner.scanFile()", e); Log.e(TAG, "RemoteException in MediaScanner.scanFile()", e);

View File

@ -21,9 +21,8 @@ package android.media;
*/ */
public interface MediaScannerClient public interface MediaScannerClient
{ {
public void scanFile(String path, long lastModified, long fileSize, boolean isDirectory); public void scanFile(String path, long lastModified, long fileSize,
boolean isDirectory, boolean noMedia);
public void addNoMediaFolder(String path);
/** /**
* Called by native code to return metadata extracted from media files. * Called by native code to return metadata extracted from media files.

View File

@ -62,13 +62,11 @@ public:
} }
else { else {
mScanFileMethodID = env->GetMethodID(mediaScannerClientInterface, "scanFile", mScanFileMethodID = env->GetMethodID(mediaScannerClientInterface, "scanFile",
"(Ljava/lang/String;JJZ)V"); "(Ljava/lang/String;JJZZ)V");
mHandleStringTagMethodID = env->GetMethodID(mediaScannerClientInterface, "handleStringTag", mHandleStringTagMethodID = env->GetMethodID(mediaScannerClientInterface, "handleStringTag",
"(Ljava/lang/String;Ljava/lang/String;)V"); "(Ljava/lang/String;Ljava/lang/String;)V");
mSetMimeTypeMethodID = env->GetMethodID(mediaScannerClientInterface, "setMimeType", mSetMimeTypeMethodID = env->GetMethodID(mediaScannerClientInterface, "setMimeType",
"(Ljava/lang/String;)V"); "(Ljava/lang/String;)V");
mAddNoMediaFolderMethodID = env->GetMethodID(mediaScannerClientInterface, "addNoMediaFolder",
"(Ljava/lang/String;)V");
} }
} }
@ -79,13 +77,13 @@ public:
// returns true if it succeeded, false if an exception occured in the Java code // returns true if it succeeded, false if an exception occured in the Java code
virtual bool scanFile(const char* path, long long lastModified, virtual bool scanFile(const char* path, long long lastModified,
long long fileSize, bool isDirectory) long long fileSize, bool isDirectory, bool noMedia)
{ {
jstring pathStr; jstring pathStr;
if ((pathStr = mEnv->NewStringUTF(path)) == NULL) return false; if ((pathStr = mEnv->NewStringUTF(path)) == NULL) return false;
mEnv->CallVoidMethod(mClient, mScanFileMethodID, pathStr, lastModified, mEnv->CallVoidMethod(mClient, mScanFileMethodID, pathStr, lastModified,
fileSize, isDirectory); fileSize, isDirectory, noMedia);
mEnv->DeleteLocalRef(pathStr); mEnv->DeleteLocalRef(pathStr);
return (!mEnv->ExceptionCheck()); return (!mEnv->ExceptionCheck());
@ -117,26 +115,12 @@ public:
return (!mEnv->ExceptionCheck()); return (!mEnv->ExceptionCheck());
} }
// returns true if it succeeded, false if an exception occured in the Java code
virtual bool addNoMediaFolder(const char* path)
{
jstring pathStr;
if ((pathStr = mEnv->NewStringUTF(path)) == NULL) return false;
mEnv->CallVoidMethod(mClient, mAddNoMediaFolderMethodID, pathStr);
mEnv->DeleteLocalRef(pathStr);
return (!mEnv->ExceptionCheck());
}
private: private:
JNIEnv *mEnv; JNIEnv *mEnv;
jobject mClient; jobject mClient;
jmethodID mScanFileMethodID; jmethodID mScanFileMethodID;
jmethodID mHandleStringTagMethodID; jmethodID mHandleStringTagMethodID;
jmethodID mSetMimeTypeMethodID; jmethodID mSetMimeTypeMethodID;
jmethodID mAddNoMediaFolderMethodID;
}; };

View File

@ -70,8 +70,7 @@ status_t MediaScanner::processDirectory(
client.setLocale(locale()); client.setLocale(locale());
status_t result = status_t result =
doProcessDirectory( doProcessDirectory(pathBuffer, pathRemaining, client, false, exceptionCheck, exceptionEnv);
pathBuffer, pathRemaining, client, exceptionCheck, exceptionEnv);
free(pathBuffer); free(pathBuffer);
@ -80,20 +79,18 @@ status_t MediaScanner::processDirectory(
status_t MediaScanner::doProcessDirectory( status_t MediaScanner::doProcessDirectory(
char *path, int pathRemaining, MediaScannerClient &client, char *path, int pathRemaining, MediaScannerClient &client,
ExceptionCheck exceptionCheck, void *exceptionEnv) { bool noMedia, ExceptionCheck exceptionCheck, void *exceptionEnv) {
// place to copy file or directory name // place to copy file or directory name
char* fileSpot = path + strlen(path); char* fileSpot = path + strlen(path);
struct dirent* entry; struct dirent* entry;
struct stat statbuf; struct stat statbuf;
// ignore directories that contain a ".nomedia" file // Treat all files as non-media in directories that contain a ".nomedia" file
if (pathRemaining >= 8 /* strlen(".nomedia") */ ) { if (pathRemaining >= 8 /* strlen(".nomedia") */ ) {
strcpy(fileSpot, ".nomedia"); strcpy(fileSpot, ".nomedia");
if (access(path, F_OK) == 0) { if (access(path, F_OK) == 0) {
LOGD("found .nomedia, skipping directory\n"); LOGD("found .nomedia, setting noMedia flag\n");
fileSpot[0] = 0; noMedia = true;
client.addNoMediaFolder(path);
return OK;
} }
// restore path // restore path
@ -138,19 +135,20 @@ status_t MediaScanner::doProcessDirectory(
} }
if (type == DT_REG || type == DT_DIR) { if (type == DT_REG || type == DT_DIR) {
if (type == DT_DIR) { if (type == DT_DIR) {
// ignore directories with a name that starts with '.' // set noMedia flag on directories with a name that starts with '.'
// for example, the Mac ".Trashes" directory // for example, the Mac ".Trashes" directory
if (name[0] == '.') continue; if (name[0] == '.')
noMedia = true;
// report the directory to the client // report the directory to the client
if (stat(path, &statbuf) == 0) { if (stat(path, &statbuf) == 0) {
client.scanFile(path, statbuf.st_mtime, 0, true); client.scanFile(path, statbuf.st_mtime, 0, true, noMedia);
} }
// and now process its contents // and now process its contents
strcat(fileSpot, "/"); strcat(fileSpot, "/");
int err = doProcessDirectory(path, pathRemaining - nameLength - 1, client, int err = doProcessDirectory(path, pathRemaining - nameLength - 1, client,
exceptionCheck, exceptionEnv); noMedia, exceptionCheck, exceptionEnv);
if (err) { if (err) {
// pass exceptions up - ignore other errors // pass exceptions up - ignore other errors
if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure; if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure;
@ -159,7 +157,7 @@ status_t MediaScanner::doProcessDirectory(
} }
} else { } else {
stat(path, &statbuf); stat(path, &statbuf);
client.scanFile(path, statbuf.st_mtime, statbuf.st_size, false); client.scanFile(path, statbuf.st_mtime, statbuf.st_size, false, noMedia);
if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure; if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure;
} }
} }