OSDN Git Service

Fix MediaScan issue w/ Deleting
authorStephen Bird <sbird@cyngn.com>
Fri, 21 Aug 2015 17:34:33 +0000 (10:34 -0700)
committerMartin Brabham <mbrabham@cyngn.com>
Fri, 21 Aug 2015 18:40:38 +0000 (11:40 -0700)
Mediascan wasn't working with deletion of items.
We need to grab the items to delete before actually
deleting them from the file system so that we know
which items we should have deleted.

Change-Id: Ia7b6d5c0612b8053d6b3f442dc9cc9312b02e47b
Ticket: QRDL-982

src/com/cyanogenmod/filemanager/util/CommandHelper.java

index eeac530..85808da 100644 (file)
@@ -23,6 +23,7 @@ import android.content.Intent;
 import android.media.MediaScannerConnection;
 import android.net.Uri;
 
+import android.util.Log;
 import com.cyanogenmod.filemanager.commands.AsyncResultListener;
 import com.cyanogenmod.filemanager.commands.ChangeOwnerExecutable;
 import com.cyanogenmod.filemanager.commands.ChangePermissionsExecutable;
@@ -100,6 +101,8 @@ import java.util.Stack;
  */
 public final class CommandHelper {
 
+    private static final String TAG = "CommandHelper";
+
     /**
      * A wrapper class for asynchronous operations that need restore the filesystem
      * after the operation.
@@ -358,6 +361,47 @@ public final class CommandHelper {
         return executable.getResult().booleanValue();
     }
 
+    private static String[] collectScanPaths(final Context context, String path) {
+        ArrayList<String> paths = new ArrayList<>();
+        Stack<FileSystemObject> pathsToScan = new Stack<>();
+        try {
+            FileSystemObject fso = getFileInfo(context, path, null);
+            if (fso == null) {
+                return new String[0];
+            }
+            pathsToScan.push(fso);
+            while (!pathsToScan.isEmpty()) {
+                fso = pathsToScan.pop();
+                paths.add(MediaHelper.normalizeMediaPath(fso.getFullPath()));
+                if (fso instanceof Directory) {
+                    List<FileSystemObject> files =
+                            CommandHelper.listFiles(context, fso.getFullPath(), null);
+                    if (files == null) {
+                        continue;
+                    }
+                    for (FileSystemObject file : files) {
+                        if (file instanceof ParentDirectory) {
+                            continue;
+                        }
+                        pathsToScan.push(file);
+                    }
+                }
+            }
+            return paths.toArray(new String[paths.size()]);
+        } catch (IOException
+                | ConsoleAllocException
+                | NoSuchFileOrDirectory
+                | InsufficientPermissionsException
+                | CommandNotFoundException
+                | OperationTimeoutException
+                | ExecutionException
+                | InvalidCommandDefinitionException e) {
+            // Just stop scanning
+            Log.e(TAG, "Recursive Delete Failed with: ", e);
+            return new String[0];
+        }
+    }
+
     /**
      * Method that deletes a directory.
      *
@@ -385,14 +429,16 @@ public final class CommandHelper {
             CommandNotFoundException, OperationTimeoutException,
             ExecutionException, InvalidCommandDefinitionException, ReadOnlyFilesystemException,
             CancelledOperationException {
+
+        String[] pathsToScan = collectScanPaths(context, directory);
+
         Console c = ensureConsoleForFile(context, console, directory);
         DeleteDirExecutable executable =
                 c.getExecutableFactory().newCreator().createDeleteDirExecutable(directory);
         writableExecute(context, executable, c);
 
-        // update media scan
-        MediaScannerConnection.scanFile(context, new String[]{
-                MediaHelper.normalizeMediaPath(directory)}, null, null);
+        // Remove from mediascanner
+        MediaScannerConnection.scanFile(context, pathsToScan, null, null);
 
         return executable.getResult().booleanValue();
     }
@@ -424,15 +470,16 @@ public final class CommandHelper {
             CommandNotFoundException, OperationTimeoutException,
             ExecutionException, InvalidCommandDefinitionException, ReadOnlyFilesystemException,
             CancelledOperationException {
+
+        String[] pathsToScan = collectScanPaths(context, file);
+
         Console c = ensureConsoleForFile(context, console, file);
         DeleteFileExecutable executable =
                 c.getExecutableFactory().newCreator().createDeleteFileExecutable(file);
         writableExecute(context, executable, c);
 
         // Remove from mediascanner
-        MediaScannerConnection.scanFile(context, new String[]{
-                MediaHelper.normalizeMediaPath(file)
-        }, null, null);
+        MediaScannerConnection.scanFile(context, pathsToScan, null, null);
 
         return executable.getResult().booleanValue();
     }