From 684c02a77303cbef162102c4d1210621a67849d5 Mon Sep 17 00:00:00 2001 From: Sudheer Shanka Date: Tue, 29 Jan 2019 17:41:15 -0800 Subject: [PATCH] Don't ignore COLUMN_MEDIA_SCANNED values from caller. Right now, DownloadProvider only uses the COLUMN_MEDIA_SCANNED value if it is coming from addCompletedDownload and for the rest of the requests, it ignores the incoming COLUMN_MEDIA_SCANNED value and always invokes mediascanner. This is not what the documentation says. For e.g., if the caller uses DownloadManager.setDestinationUri() API, then unless otherwise specified, the download doesn't need to be mediascanned. Also, since we are inserting user visible downloads to MediaProvider, use that info to populate the COLUMN_MEDIAPROVIDER_URI column as well and update DownloadProvider to not invoke MediaScanner. Bug: 123440050 Test: atest DownloadProviderTests Test: atest cts/tests/app/src/android/app/cts/DownloadManagerTest.java Test: atest cts/tests/tests/provider/src/android/provider/cts/MediaStore* Change-Id: I65ccae672eabe2efd5132d4b105c18bb591379fc --- core/java/android/app/DownloadManager.java | 25 ++++------------------ core/java/android/provider/Downloads.java | 10 ++++++++- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/core/java/android/app/DownloadManager.java b/core/java/android/app/DownloadManager.java index acc70944abf03..fde1d29eb709c 100644 --- a/core/java/android/app/DownloadManager.java +++ b/core/java/android/app/DownloadManager.java @@ -26,7 +26,6 @@ import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; -import android.content.Intent; import android.database.Cursor; import android.database.CursorWrapper; import android.net.ConnectivityManager; @@ -37,7 +36,6 @@ import android.os.Environment; import android.os.FileUtils; import android.os.ParcelFileDescriptor; import android.provider.Downloads; -import android.provider.MediaStore.Images; import android.provider.Settings; import android.provider.Settings.SettingNotFoundException; import android.text.TextUtils; @@ -399,14 +397,14 @@ public class DownloadManager { /** if a file is designated as a MediaScanner scannable file, the following value is * stored in the database column {@link Downloads.Impl#COLUMN_MEDIA_SCANNED}. */ - private static final int SCANNABLE_VALUE_YES = 0; + private static final int SCANNABLE_VALUE_YES = Downloads.Impl.MEDIA_NOT_SCANNED; // value of 1 is stored in the above column by DownloadProvider after it is scanned by // MediaScanner /** if a file is designated as a file that should not be scanned by MediaScanner, * the following value is stored in the database column * {@link Downloads.Impl#COLUMN_MEDIA_SCANNED}. */ - private static final int SCANNABLE_VALUE_NO = 2; + private static final int SCANNABLE_VALUE_NO = Downloads.Impl.MEDIA_NOT_SCANNABLE; /** * This download is visible but only shows in the notifications @@ -1264,19 +1262,6 @@ public class DownloadManager { throw new IllegalStateException("Failed to rename to " + after); } - // Update MediaProvider if necessary - if (mimeType.startsWith("image/")) { - context.getContentResolver().delete(Images.Media.EXTERNAL_CONTENT_URI, - Images.Media.DATA + "=?", - new String[] { - before.getAbsolutePath() - }); - - Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); - intent.setData(Uri.fromFile(after)); - context.sendBroadcast(intent); - } - ContentValues values = new ContentValues(); values.put(Downloads.Impl.COLUMN_TITLE, displayName); values.put(Downloads.Impl._DATA, after.toString()); @@ -1329,8 +1314,7 @@ public class DownloadManager { * @param description the description that would appear for this file in Downloads App. * @param isMediaScannerScannable true if the file is to be scanned by MediaScanner. Files * scanned by MediaScanner appear in the applications used to view media (for example, - * Gallery app). Starting from {@link android.os.Build.VERSION_CODES#Q}, this argument is - * ignored and the file is always scanned by MediaScanner. + * Gallery app). * @param mimeType mimetype of the file. * @param path absolute pathname to the file. The file should be world-readable, so that it can * be managed by the Downloads App and any other app that is used to read it (for example, @@ -1359,8 +1343,7 @@ public class DownloadManager { * @param description the description that would appear for this file in Downloads App. * @param isMediaScannerScannable true if the file is to be scanned by MediaScanner. Files * scanned by MediaScanner appear in the applications used to view media (for example, - * Gallery app). Starting from {@link android.os.Build.VERSION_CODES#Q}, this argument is - * ignored and the file is always scanned by MediaScanner. + * Gallery app). * @param mimeType mimetype of the file. * @param path absolute pathname to the file. The file should be world-readable, so that it can * be managed by the Downloads App and any other app that is used to read it (for example, diff --git a/core/java/android/provider/Downloads.java b/core/java/android/provider/Downloads.java index 63bbb9c0bc123..89d1c447a7095 100644 --- a/core/java/android/provider/Downloads.java +++ b/core/java/android/provider/Downloads.java @@ -419,12 +419,20 @@ public final class Downloads { /** * The column that is used to remember whether the media scanner was invoked. - * It can take the values: null or 0(not scanned), 1(scanned), 2 (not scannable). + * It can take the values: {@link #MEDIA_NOT_SCANNED}, {@link #MEDIA_SCANNED} or + * {@link #MEDIA_NOT_SCANNABLE} or {@code null}. If it's value is {@code null}, it will be + * treated as {@link #MEDIA_NOT_SCANNED}. + * *

Type: TEXT

*/ @UnsupportedAppUsage public static final String COLUMN_MEDIA_SCANNED = "scanned"; + /** Possible values for column {@link #COLUMN_MEDIA_SCANNED} */ + public static final int MEDIA_NOT_SCANNED = 0; + public static final int MEDIA_SCANNED = 1; + public static final int MEDIA_NOT_SCANNABLE = 2; + /** * The column with errorMsg for a failed downloaded. * Used only for debugging purposes.