diff options
Diffstat (limited to 'src/com/android')
3 files changed, 42 insertions, 8 deletions
diff --git a/src/com/android/providers/downloads/DownloadProvider.java b/src/com/android/providers/downloads/DownloadProvider.java index a4bc9fef..3f23e848 100644 --- a/src/com/android/providers/downloads/DownloadProvider.java +++ b/src/com/android/providers/downloads/DownloadProvider.java @@ -42,6 +42,7 @@ import android.database.DatabaseUtils; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import android.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; import android.os.Binder; import android.os.ParcelFileDescriptor; @@ -881,8 +882,6 @@ public final class DownloadProvider extends ContentProvider { final String selection, final String[] selectionArgs, final String sort) { - Helpers.validateSelection(selection, sAppReadableColumnsSet); - SQLiteDatabase db = mOpenHelper.getReadableDatabase(); int match = sURIMatcher.match(uri); @@ -929,7 +928,10 @@ public final class DownloadProvider extends ContentProvider { logVerboseQueryInfo(projection, selection, selectionArgs, sort, db); } - Cursor ret = db.query(DB_TABLE, projection, fullSelection.getSelection(), + SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); + builder.setTables(DB_TABLE); + builder.setStrict(true); + Cursor ret = builder.query(db, projection, fullSelection.getSelection(), fullSelection.getParameters(), null, null, sort); if (ret != null) { diff --git a/src/com/android/providers/downloads/DownloadStorageProvider.java b/src/com/android/providers/downloads/DownloadStorageProvider.java index e0bb7cd1..fb9392f9 100644 --- a/src/com/android/providers/downloads/DownloadStorageProvider.java +++ b/src/com/android/providers/downloads/DownloadStorageProvider.java @@ -99,8 +99,8 @@ public class DownloadStorageProvider extends DocumentsProvider { final MatrixCursor result = new MatrixCursor(resolveRootProjection(projection)); final RowBuilder row = result.newRow(); row.add(Root.COLUMN_ROOT_ID, DOC_ID_ROOT); - row.add(Root.COLUMN_FLAGS, - Root.FLAG_LOCAL_ONLY | Root.FLAG_SUPPORTS_RECENTS | Root.FLAG_SUPPORTS_CREATE); + row.add(Root.COLUMN_FLAGS, Root.FLAG_LOCAL_ONLY | Root.FLAG_SUPPORTS_RECENTS + | Root.FLAG_SUPPORTS_CREATE | Root.FLAG_SUPPORTS_SEARCH); row.add(Root.COLUMN_ICON, R.mipmap.ic_launcher_download); row.add(Root.COLUMN_TITLE, getContext().getString(R.string.root_downloads)); row.add(Root.COLUMN_DOCUMENT_ID, DOC_ID_ROOT); @@ -292,6 +292,28 @@ public class DownloadStorageProvider extends DocumentsProvider { } @Override + public Cursor querySearchDocuments(String rootId, String query, String[] projection) + throws FileNotFoundException { + final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection)); + + // Delegate to real provider + final long token = Binder.clearCallingIdentity(); + Cursor cursor = null; + try { + cursor = mDm.query(new DownloadManager.Query().setOnlyIncludeVisibleInDownloadsUi(true) + .setFilterByString(query)); + copyNotificationUri(result, cursor); + while (cursor.moveToNext()) { + includeDownloadFromCursor(result, cursor); + } + } finally { + IoUtils.closeQuietly(cursor); + Binder.restoreCallingIdentity(token); + } + return result; + } + + @Override public ParcelFileDescriptor openDocument(String docId, String mode, CancellationSignal signal) throws FileNotFoundException { if (mArchiveHelper.isArchivedDocument(docId)) { @@ -325,6 +347,10 @@ public class DownloadStorageProvider extends DocumentsProvider { Document.FLAG_DIR_PREFERS_LAST_MODIFIED | Document.FLAG_DIR_SUPPORTS_CREATE); } + /** + * Adds the entry from the cursor to the result only if the entry is valid. That is, + * if the file exists in the file system. + */ private void includeDownloadFromCursor(MatrixCursor result, Cursor cursor) { final long id = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID)); final String docId = String.valueOf(id); @@ -344,12 +370,20 @@ public class DownloadStorageProvider extends DocumentsProvider { if (size == -1) { size = null; } + String localFilePath = cursor.getString( + cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_FILENAME)); int extraFlags = Document.FLAG_PARTIAL; final int status = cursor.getInt( cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)); switch (status) { case DownloadManager.STATUS_SUCCESSFUL: + // Verify that the document still exists in external storage. This is necessary + // because files can be deleted from the file system without their entry being + // removed from DownloadsManager. + if (localFilePath == null || !new File(localFilePath).exists()) { + return; + } extraFlags = Document.FLAG_SUPPORTS_RENAME; // only successful is non-partial break; case DownloadManager.STATUS_PAUSED: @@ -400,8 +434,6 @@ public class DownloadStorageProvider extends DocumentsProvider { row.add(Document.COLUMN_LAST_MODIFIED, lastModified); } - final String localFilePath = cursor.getString( - cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_FILENAME)); if (localFilePath != null) { row.add(DocumentArchiveHelper.COLUMN_LOCAL_FILE_PATH, localFilePath); } diff --git a/src/com/android/providers/downloads/StorageUtils.java b/src/com/android/providers/downloads/StorageUtils.java index 3bb57c8e..d7a5c33b 100644 --- a/src/com/android/providers/downloads/StorageUtils.java +++ b/src/com/android/providers/downloads/StorageUtils.java @@ -154,7 +154,7 @@ public class StorageUtils { Collections.sort(files, new Comparator<ConcreteFile>() { @Override public int compare(ConcreteFile lhs, ConcreteFile rhs) { - return (int) (lhs.file.lastModified() - rhs.file.lastModified()); + return Long.compare(lhs.file.lastModified(), rhs.file.lastModified()); } }); |