Merge "Don't treat failed scans as valid media files" into pi-dev

This commit is contained in:
Marco Nelissen
2018-04-27 21:35:33 +00:00
committed by Android (Google) Code Review
2 changed files with 31 additions and 21 deletions

View File

@@ -488,6 +488,7 @@ public class MediaScanner implements AutoCloseable {
private int mCompilation;
private boolean mIsDrm;
private boolean mNoMedia; // flag to suppress file from appearing in media tables
private boolean mScanSuccess;
private int mWidth;
private int mHeight;
@@ -502,6 +503,7 @@ public class MediaScanner implements AutoCloseable {
mFileType = 0;
mFileSize = fileSize;
mIsDrm = false;
mScanSuccess = true;
if (!isDirectory) {
if (!noMedia && isNoMediaFile(path)) {
@@ -623,14 +625,6 @@ public class MediaScanner implements AutoCloseable {
if (noMedia) {
result = endFile(entry, false, false, false, false, false);
} else {
String lowpath = path.toLowerCase(Locale.ROOT);
boolean ringtones = (lowpath.indexOf(RINGTONES_DIR) > 0);
boolean notifications = (lowpath.indexOf(NOTIFICATIONS_DIR) > 0);
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);
boolean isaudio = MediaFile.isAudioFileType(mFileType);
boolean isvideo = MediaFile.isVideoFileType(mFileType);
boolean isimage = MediaFile.isImageFileType(mFileType);
@@ -642,13 +636,22 @@ public class MediaScanner implements AutoCloseable {
// we only extract metadata for audio and video files
if (isaudio || isvideo) {
processFile(path, mimeType, this);
mScanSuccess = processFile(path, mimeType, this);
}
if (isimage) {
processImageFile(path);
mScanSuccess = processImageFile(path);
}
String lowpath = path.toLowerCase(Locale.ROOT);
boolean ringtones = mScanSuccess && (lowpath.indexOf(RINGTONES_DIR) > 0);
boolean notifications = mScanSuccess &&
(lowpath.indexOf(NOTIFICATIONS_DIR) > 0);
boolean alarms = mScanSuccess && (lowpath.indexOf(ALARMS_DIR) > 0);
boolean podcasts = mScanSuccess && (lowpath.indexOf(PODCAST_DIR) > 0);
boolean music = mScanSuccess && ((lowpath.indexOf(MUSIC_DIR) > 0) ||
(!ringtones && !notifications && !alarms && !podcasts));
result = endFile(entry, ringtones, notifications, alarms, music, podcasts);
}
}
@@ -816,16 +819,18 @@ public class MediaScanner implements AutoCloseable {
return genreTagValue;
}
private void processImageFile(String path) {
private boolean processImageFile(String path) {
try {
mBitmapOptions.outWidth = 0;
mBitmapOptions.outHeight = 0;
BitmapFactory.decodeFile(path, mBitmapOptions);
mWidth = mBitmapOptions.outWidth;
mHeight = mBitmapOptions.outHeight;
return mWidth > 0 && mHeight > 0;
} catch (Throwable th) {
// ignore;
}
return false;
}
public void setMimeType(String mimeType) {
@@ -878,7 +883,7 @@ public class MediaScanner implements AutoCloseable {
}
} else if (MediaFile.isImageFileType(mFileType)) {
// FIXME - add DESCRIPTION
} else if (MediaFile.isAudioFileType(mFileType)) {
} else if (mScanSuccess && MediaFile.isAudioFileType(mFileType)) {
map.put(Audio.Media.ARTIST, (mArtist != null && mArtist.length() > 0) ?
mArtist : MediaStore.UNKNOWN_STRING);
map.put(Audio.Media.ALBUM_ARTIST, (mAlbumArtist != null &&
@@ -894,6 +899,10 @@ public class MediaScanner implements AutoCloseable {
map.put(Audio.Media.DURATION, mDuration);
map.put(Audio.Media.COMPILATION, mCompilation);
}
if (!mScanSuccess) {
// force mediaprovider to not determine the media type from the mime type
map.put(Files.FileColumns.MEDIA_TYPE, 0);
}
}
return map;
}
@@ -1001,7 +1010,7 @@ public class MediaScanner implements AutoCloseable {
Uri tableUri = mFilesUri;
MediaInserter inserter = mMediaInserter;
if (!mNoMedia) {
if (mScanSuccess && !mNoMedia) {
if (MediaFile.isVideoFileType(mFileType)) {
tableUri = mVideoUri;
} else if (MediaFile.isImageFileType(mFileType)) {
@@ -1071,7 +1080,7 @@ public class MediaScanner implements AutoCloseable {
values.remove(MediaStore.MediaColumns.DATA);
int mediaType = 0;
if (!MediaScanner.isNoMediaPath(entry.mPath)) {
if (mScanSuccess && !MediaScanner.isNoMediaPath(entry.mPath)) {
int fileType = MediaFile.getFileTypeForMimeType(mMimeType);
if (MediaFile.isAudioFileType(fileType)) {
mediaType = FileColumns.MEDIA_TYPE_AUDIO;
@@ -1890,7 +1899,7 @@ public class MediaScanner implements AutoCloseable {
}
private native void processDirectory(String path, MediaScannerClient client);
private native void processFile(String path, String mimeType, MediaScannerClient client);
private native boolean processFile(String path, String mimeType, MediaScannerClient client);
private native void setLocale(String locale);
public native byte[] extractAlbumArt(FileDescriptor fd);

View File

@@ -264,7 +264,7 @@ android_media_MediaScanner_processDirectory(
env->ReleaseStringUTFChars(path, pathStr);
}
static void
static jboolean
android_media_MediaScanner_processFile(
JNIEnv *env, jobject thiz, jstring path,
jstring mimeType, jobject client)
@@ -275,17 +275,17 @@ android_media_MediaScanner_processFile(
MediaScanner *mp = getNativeScanner_l(env, thiz);
if (mp == NULL) {
jniThrowException(env, kRunTimeException, "No scanner available");
return;
return false;
}
if (path == NULL) {
jniThrowException(env, kIllegalArgumentException, NULL);
return;
return false;
}
const char *pathStr = env->GetStringUTFChars(path, NULL);
if (pathStr == NULL) { // Out of memory
return;
return false;
}
const char *mimeTypeStr =
@@ -293,7 +293,7 @@ android_media_MediaScanner_processFile(
if (mimeType && mimeTypeStr == NULL) { // Out of memory
// ReleaseStringUTFChars can be called with an exception pending.
env->ReleaseStringUTFChars(path, pathStr);
return;
return false;
}
MyMediaScannerClient myClient(env, client);
@@ -305,6 +305,7 @@ android_media_MediaScanner_processFile(
if (mimeType) {
env->ReleaseStringUTFChars(mimeType, mimeTypeStr);
}
return result != MEDIA_SCAN_RESULT_ERROR;
}
static void
@@ -421,7 +422,7 @@ static const JNINativeMethod gMethods[] = {
{
"processFile",
"(Ljava/lang/String;Ljava/lang/String;Landroid/media/MediaScannerClient;)V",
"(Ljava/lang/String;Ljava/lang/String;Landroid/media/MediaScannerClient;)Z",
(void *)android_media_MediaScanner_processFile
},