Use 'like' to do fileglobbing in androids media db

GLOB seems to have way too much trouble with [] chars.
We also make sure that directory globs are terminated with a /
(/foo/media should not match /foo/medias)
This commit is contained in:
Adrian Ulrich 2013-05-16 15:33:43 +02:00
parent d98322b122
commit b178023900

View File

@ -532,8 +532,8 @@ public class MediaUtils {
if((new File(pfx+"/Android")).lastModified() == exLastmod) { if((new File(pfx+"/Android")).lastModified() == exLastmod) {
String guessPath = exPath + path.substring(pfx.length()); String guessPath = exPath + path.substring(pfx.length());
if( (new File(guessPath)).exists() ) { if( (new File(guessPath)).exists() ) {
Log.v("Vanilla", "OLD="+path); Log.d("Vanilla", "OLD="+path);
Log.v("Vanilla", "NEW="+guessPath); Log.d("Vanilla", "NEW="+guessPath);
path = guessPath; path = guessPath;
break; break;
} }
@ -549,7 +549,17 @@ public class MediaUtils {
return path; return path;
} }
/**
* Adds a final slash if the path points to an existing directory
*/
private static String addDirEndSlash(String path) {
if(path.length() > 0 && path.charAt(path.length()-1) != '/') {
if( (new File(path)).isDirectory() ) {
path += "/";
}
}
return path;
}
/** /**
* Build a query that will contain all the media under the given path. * Build a query that will contain all the media under the given path.
@ -560,18 +570,18 @@ public class MediaUtils {
*/ */
public static QueryTask buildFileQuery(String path, String[] projection) public static QueryTask buildFileQuery(String path, String[] projection)
{ {
// It would be better to use selectionArgs to pass path here, but there /* make sure that the path is:
// doesn't appear to be any way to pass the * when using it. -> fixed-up to point to the real mountpoint if user browsed to the mediadir symlink
Log.v("Vanilla", "buildFileQuery "+path); -> terminated with a / if it is a directory
StringBuilder selection = new StringBuilder(); -> ended with a % for the LIKE query
selection.append("_data GLOB "); */
DatabaseUtils.appendEscapedSQLString(selection, sanitizeMediaPath(path)); path = addDirEndSlash(sanitizeMediaPath(path)) + "%";
// delete the quotation mark added by the escape method final String query = "_data LIKE ? AND is_music";
selection.deleteCharAt(selection.length() - 1); String[] qargs = { path };
selection.append("*' AND is_music");
Log.d("VanillaMusic", "Running buildFileQuery for "+query+" with ARG="+qargs[0]);
Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
QueryTask result = new QueryTask(media, projection, selection.toString(), null, DEFAULT_SORT); QueryTask result = new QueryTask(media, projection, query, qargs, DEFAULT_SORT);
result.type = TYPE_FILE; result.type = TYPE_FILE;
return result; return result;
} }