MediaProvider: Allow scan for directories

Patchset 2: Allow only directories of external volumes

Change-Id: I5694f75896749d84b163a1213730eaa4c1e776e6
Signed-off-by: Jorge Ruesga <jorge@ruesga.com>
diff --git a/src/com/android/providers/media/MediaScannerService.java b/src/com/android/providers/media/MediaScannerService.java
index 40bb10a..81a4289 100644
--- a/src/com/android/providers/media/MediaScannerService.java
+++ b/src/com/android/providers/media/MediaScannerService.java
@@ -186,13 +186,18 @@
         Looper.loop();
     }
    
-    private Uri scanFile(String path, String mimeType) {
+    private Uri scanFileOrDirectory(String path, String mimeType) {
         String volumeName = MediaProvider.EXTERNAL_VOLUME;
         openDatabase(volumeName);
         MediaScanner scanner = createMediaScanner();
         try {
             // make sure the file path is in canonical form
-            String canonicalPath = new File(path).getCanonicalPath();
+            File file = new File(path);
+            String canonicalPath = file.getCanonicalPath();
+            if (file.isDirectory() && isExternalVolumeDirectory(canonicalPath)) {
+                scanner.scanDirectories(new String[]{canonicalPath}, volumeName);
+                return Uri.fromFile(file);
+            }
             return scanner.scanSingleFile(canonicalPath, volumeName, mimeType);
         } catch (Exception e) {
             Log.e(TAG, "bad path " + path + " in scanFile()", e);
@@ -200,6 +205,15 @@
         }
     }
 
+    private boolean isExternalVolumeDirectory(String path) {
+        for (String directory : mExternalStoragePaths) {
+            if (path.startsWith(directory)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     @Override
     public IBinder onBind(Intent intent)
     {
@@ -243,7 +257,7 @@
                             (binder == null ? null : IMediaScannerListener.Stub.asInterface(binder));
                     Uri uri = null;
                     try {
-                        uri = scanFile(filePath, arguments.getString("mimetype"));
+                        uri = scanFileOrDirectory(filePath, arguments.getString("mimetype"));
                     } catch (Exception e) {
                         Log.e(TAG, "Exception scanning file", e);
                     }