Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d5372f98 authored by Stephen Bird's avatar Stephen Bird Committed by Martin Brabham
Browse files

Fix MediaScan issue w/ Deleting

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
parent 530d6f62
Loading
Loading
Loading
Loading
+53 −6
Original line number Diff line number Diff line
@@ -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();
    }