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

Commit b0745759 authored by Garfield Tan's avatar Garfield Tan Committed by Android (Google) Code Review
Browse files

Merge "Check visible paths as well when generate docId from file system path."

parents 353cad6d dc9593e7
Loading
Loading
Loading
Loading
+36 −19
Original line number Original line Diff line number Diff line
@@ -287,26 +287,23 @@ public class ExternalStorageProvider extends DocumentsProvider {
        String path = file.getAbsolutePath();
        String path = file.getAbsolutePath();


        // Find the most-specific root path
        // Find the most-specific root path
        String mostSpecificId = null;
        boolean visiblePath = false;
        String mostSpecificPath = null;
        RootInfo mostSpecificRoot = getMostSpecificRootForPath(path, false);
        synchronized (mRootsLock) {

            for (int i = 0; i < mRoots.size(); i++) {
        if (mostSpecificRoot == null) {
                final String rootId = mRoots.keyAt(i);
            // Try visible path if no internal path matches. MediaStore uses visible paths.
                final String rootPath = mRoots.valueAt(i).path.getAbsolutePath();
            visiblePath = true;
                if (path.startsWith(rootPath) && (mostSpecificPath == null
            mostSpecificRoot = getMostSpecificRootForPath(path, true);
                        || rootPath.length() > mostSpecificPath.length())) {
                    mostSpecificId = rootId;
                    mostSpecificPath = rootPath;
                }
            }
        }
        }


        if (mostSpecificPath == null) {
        if (mostSpecificRoot == null) {
            throw new FileNotFoundException("Failed to find root that contains " + path);
            throw new FileNotFoundException("Failed to find root that contains " + path);
        }
        }


        // Start at first char of path under root
        // Start at first char of path under root
        final String rootPath = mostSpecificPath;
        final String rootPath = visiblePath
                ? mostSpecificRoot.visiblePath.getAbsolutePath()
                : mostSpecificRoot.path.getAbsolutePath();
        if (rootPath.equals(path)) {
        if (rootPath.equals(path)) {
            path = "";
            path = "";
        } else if (rootPath.endsWith("/")) {
        } else if (rootPath.endsWith("/")) {
@@ -322,7 +319,29 @@ public class ExternalStorageProvider extends DocumentsProvider {
            }
            }
        }
        }


        return mostSpecificId + ':' + path;
        return mostSpecificRoot.rootId + ':' + path;
    }

    private RootInfo getMostSpecificRootForPath(String path, boolean visible) {
        // Find the most-specific root path
        RootInfo mostSpecificRoot = null;
        String mostSpecificPath = null;
        synchronized (mRootsLock) {
            for (int i = 0; i < mRoots.size(); i++) {
                final RootInfo root = mRoots.valueAt(i);
                final File rootFile = visible ? root.visiblePath : root.path;
                if (rootFile != null) {
                    final String rootPath = rootFile.getAbsolutePath();
                    if (path.startsWith(rootPath) && (mostSpecificPath == null
                            || rootPath.length() > mostSpecificPath.length())) {
                        mostSpecificRoot = root;
                        mostSpecificPath = rootPath;
                    }
                }
            }
        }

        return mostSpecificRoot;
    }
    }


    private File getFileForDocId(String docId) throws FileNotFoundException {
    private File getFileForDocId(String docId) throws FileNotFoundException {
@@ -519,15 +538,13 @@ public class ExternalStorageProvider extends DocumentsProvider {
                boolean matchesRequestedDoc = false;
                boolean matchesRequestedDoc = false;
                if (DocumentsContract.isTreeUri(uri)) {
                if (DocumentsContract.isTreeUri(uri)) {
                    final String parentDocId = DocumentsContract.getTreeDocumentId(uri);
                    final String parentDocId = DocumentsContract.getTreeDocumentId(uri);
                    File parentFile = getFileForDocId(parentDocId);
                    if (isChildDocument(parentDocId, docId)) {
                    if (FileUtils.contains(parentFile, doc)) {
                        treeUriPermission = uriPermission;
                        treeUriPermission = uriPermission;
                        matchesRequestedDoc = true;
                        matchesRequestedDoc = true;
                    }
                    }
                } else {
                } else {
                    final String candidateDocId = DocumentsContract.getDocumentId(uri);
                    final String candidateDocId = DocumentsContract.getDocumentId(uri);
                    final File candidateDoc = getFileForDocId(candidateDocId);
                    if (Objects.equals(docId, candidateDocId)) {
                    if (Objects.equals(doc.getAbsolutePath(), candidateDoc.getAbsolutePath())) {
                        docUriPermission = uriPermission;
                        docUriPermission = uriPermission;
                        matchesRequestedDoc = true;
                        matchesRequestedDoc = true;
                    }
                    }