Scan completed downloads when requested.
Jeff Sharkey [Mon, 9 May 2016 20:51:23 +0000 (14:51 -0600)]
The recent JobScheduler rewrite means we no longer spin up a service
when insterting an already-completed download.  However, the calling
app may have requested the download to be scanned, so kick off a
scan request for them.

Bug: 28659693
Change-Id: I497e10995ba04f1522fe8d7e547ebea6e305f6e9

src/com/android/providers/downloads/DownloadProvider.java
src/com/android/providers/downloads/DownloadScanner.java

index 00ed043..4b83cac 100644 (file)
 package com.android.providers.downloads;
 
 import static android.provider.BaseColumns._ID;
+import static android.provider.Downloads.Impl.COLUMN_DESTINATION;
 import static android.provider.Downloads.Impl.COLUMN_MEDIAPROVIDER_URI;
+import static android.provider.Downloads.Impl.COLUMN_MEDIA_SCANNED;
+import static android.provider.Downloads.Impl.COLUMN_MIME_TYPE;
+import static android.provider.Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD;
 import static android.provider.Downloads.Impl._DATA;
 
 import android.app.AppOpsManager;
@@ -695,6 +699,12 @@ public final class DownloadProvider extends ContentProvider {
             Binder.restoreCallingIdentity(token);
         }
 
+        if (values.getAsInteger(COLUMN_DESTINATION) == DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD
+                && values.getAsInteger(COLUMN_MEDIA_SCANNED) == 0) {
+            DownloadScanner.requestScanBlocking(getContext(), rowID, values.getAsString(_DATA),
+                    values.getAsString(COLUMN_MIME_TYPE));
+        }
+
         return ContentUris.withAppendedId(Downloads.Impl.CONTENT_URI, rowID);
     }
 
index 37f5114..4a5ba87 100644 (file)
@@ -76,9 +76,13 @@ public class DownloadScanner implements MediaScannerConnectionClient {
     }
 
     public static void requestScanBlocking(Context context, DownloadInfo info) {
+        requestScanBlocking(context, info.mId, info.mFileName, info.mMimeType);
+    }
+
+    public static void requestScanBlocking(Context context, long id, String path, String mimeType) {
         final DownloadScanner scanner = new DownloadScanner(context);
         scanner.mLatch = new CountDownLatch(1);
-        scanner.requestScan(info);
+        scanner.requestScan(new ScanRequest(id, path, mimeType));
         try {
             scanner.mLatch.await(SCAN_TIMEOUT, TimeUnit.MILLISECONDS);
         } catch (InterruptedException e) {
@@ -115,10 +119,9 @@ public class DownloadScanner implements MediaScannerConnectionClient {
      *
      * @see #hasPendingScans()
      */
-    public void requestScan(DownloadInfo info) {
-        if (LOGV) Log.v(TAG, "requestScan() for " + info.mFileName);
+    public void requestScan(ScanRequest req) {
+        if (LOGV) Log.v(TAG, "requestScan() for " + req.path);
         synchronized (mConnection) {
-            final ScanRequest req = new ScanRequest(info.mId, info.mFileName, info.mMimeType);
             mPending.put(req.path, req);
 
             if (mConnection.isConnected()) {