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

Commit 93615419 authored by Garfield Tan's avatar Garfield Tan
Browse files

Add special URI to MediaStore for directories.

Bug: 36048049
Bug: 31001477
Change-Id: I69032481271b7f53c22403c3526560e6830bd9e8
(cherry picked from commit 1ca2837111c8383f9c8daf1420356998f4fb218f)
parent 7ba04df3
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -533,6 +533,14 @@ public final class MediaStore {
                    + "/object/" + fileId + "/references");
        }

        /**
         * Used to trigger special logic for directories.
         * @hide
         */
        public static final Uri getDirectoryUri(String volumeName) {
            return Uri.parse(CONTENT_AUTHORITY_SLASH + volumeName + "/dir");
        }

        /**
         * Fields for master table for all media files.
         * Table also contains MediaColumns._ID, DATA, SIZE and DATE_MODIFIED.
+18 −2
Original line number Diff line number Diff line
@@ -129,21 +129,35 @@ public abstract class FileSystemProvider extends DocumentsProvider {
        }

        final File file = FileUtils.buildUniqueFile(parent, mimeType, displayName);
        final String childId;
        if (Document.MIME_TYPE_DIR.equals(mimeType)) {
            if (!file.mkdir()) {
                throw new IllegalStateException("Failed to mkdir " + file);
            }
            childId = getDocIdForFile(file);
            addFolderToMediaStore(getFileForDocId(childId, true));
        } else {
            try {
                if (!file.createNewFile()) {
                    throw new IllegalStateException("Failed to touch " + file);
                }
                childId = getDocIdForFile(file);
            } catch (IOException e) {
                throw new IllegalStateException("Failed to touch " + file + ": " + e);
            }
        }

        return getDocIdForFile(file);
        return childId;
    }

    private void addFolderToMediaStore(File visibleFolder) {
        assert(visibleFolder.isDirectory());

        final ContentResolver resolver = getContext().getContentResolver();
        final Uri uri = MediaStore.Files.getDirectoryUri("external");
        ContentValues values = new ContentValues();
        values.put(MediaStore.Files.FileColumns.DATA, visibleFolder.getAbsolutePath());
        resolver.insert(uri, values);
    }

    @Override
@@ -193,7 +207,9 @@ public abstract class FileSystemProvider extends DocumentsProvider {
    private void moveInMediaStore(File oldVisibleFile, File newVisibleFile) {
        if (newVisibleFile != null) {
            final ContentResolver resolver = getContext().getContentResolver();
            final Uri externalUri = MediaStore.Files.getContentUri("external");
            final Uri externalUri = newVisibleFile.isDirectory()
                    ? MediaStore.Files.getDirectoryUri("external")
                    : MediaStore.Files.getContentUri("external");

            ContentValues values = new ContentValues();
            values.put(MediaStore.Files.FileColumns.DATA, newVisibleFile.getAbsolutePath());