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

Commit 829a57c9 authored by Marco Nelissen's avatar Marco Nelissen Committed by Android (Google) Code Review
Browse files

Merge "Cache media/nomedia paths"

parents 33e77b61 93a5845d
Loading
Loading
Loading
Loading
+50 −16
Original line number Original line Diff line number Diff line
@@ -55,6 +55,7 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Iterator;
import java.util.Locale;
import java.util.Locale;
@@ -1400,13 +1401,41 @@ public class MediaScanner
        return false;
        return false;
    }
    }


    public static boolean isNoMediaPath(String path) {
    private static HashMap<String,String> mNoMediaPaths = new HashMap<String,String>();
        if (path == null) return false;
    private static HashMap<String,String> mMediaPaths = new HashMap<String,String>();


    /* MediaProvider calls this when a .nomedia file is added or removed */
    public static void clearMediaPathCache(boolean clearMediaPaths, boolean clearNoMediaPaths) {
        synchronized (MediaScanner.class) {
            if (clearMediaPaths) {
                mMediaPaths.clear();
            }
            if (clearNoMediaPaths) {
                mNoMediaPaths.clear();
            }
        }
    }

    public static boolean isNoMediaPath(String path) {
        if (path == null) {
            return false;
        }
        // return true if file or any parent directory has name starting with a dot
        // return true if file or any parent directory has name starting with a dot
        if (path.indexOf("/.") >= 0) return true;
        if (path.indexOf("/.") >= 0) {
            return true;
        }

        int firstSlash = path.lastIndexOf('/');
        if (firstSlash == 0) {
            return false;
        }
        String parent = path.substring(0,  firstSlash);


        // now check to see if any parent directories have a ".nomedia" file
        synchronized (MediaScanner.class) {
            if (mNoMediaPaths.containsKey(parent)) {
                return true;
            } else if (!mMediaPaths.containsKey(parent)) {
                // check to see if any parent directories have a ".nomedia" file
                // start from 1 so we don't bother checking in the root directory
                // start from 1 so we don't bother checking in the root directory
                int offset = 1;
                int offset = 1;
                while (offset >= 0) {
                while (offset >= 0) {
@@ -1416,11 +1445,16 @@ public class MediaScanner
                        File file = new File(path.substring(0, slashIndex) + ".nomedia");
                        File file = new File(path.substring(0, slashIndex) + ".nomedia");
                        if (file.exists()) {
                        if (file.exists()) {
                            // we have a .nomedia in one of the parent directories
                            // we have a .nomedia in one of the parent directories
                            mNoMediaPaths.put(parent, "");
                            return true;
                            return true;
                        }
                        }
                    }
                    }
                    offset = slashIndex;
                    offset = slashIndex;
                }
                }
                mMediaPaths.put(parent, "");
            }
        }

        return isNoMediaFile(path);
        return isNoMediaFile(path);
    }
    }