コード例 #1
0
  /*
   * @deprecated
   *
   * Search file of specific type in a directory
   * recursively and make sure reserved dirs be searched if provided This
   * method might be time-consuming, so you can use #relaySearchFilesOfType to
   * get search right after one level directory scanned.
   */
  public boolean searchFilesOfType(
      String topDir, int type, ArrayList<String> result, ArrayList<String> reservedSearchDirs) {
    ArrayList<String> typeList = null;
    switch (type) {
      case StorageUtil.TYPE_AUDIO:
        typeList = DataUtil.getSupportedAudioFileExtensions();
        break;
      case StorageUtil.TYPE_VIDEO:
        typeList = DataUtil.getSupportedVideoFileExtensions();
        break;
      case StorageUtil.TYPE_IMAGE:
        typeList = DataUtil.getSupportedImageFileExtensions();
        break;
      case StorageUtil.TYPE_APKFILE:
        typeList = DataUtil.getSupportedAppInstallerFileExtensions();
        break;
      default:
        // do nothing
        return false;
    }

    File root_dir = new File(topDir);
    String[] list = root_dir.list();

    if (list != null && root_dir.canRead()) {
      int len = list.length;

      for (int i = 0; i < len; i++) {
        File check = new File(topDir + "/" + list[i]);

        // skip symbol link and hidden files
        try {
          if (isSymlink(check) || check.isHidden()) {
            continue;
          }
        } catch (IOException e) {
          Log.e(TAG, "Failed to check symbol link!");
        }

        if (check.isFile()) {
          String extension = DataUtil.getFileExtensionWithoutDot(check.getPath());
          if (typeList.contains(extension.toLowerCase())) {
            result.add(check.getPath());
          }
        }
        if (check.isDirectory()) {
          if (StorageUtil.getExcludeSearchPath().contains(check.getAbsolutePath())
              || StorageUtil.isExcludeSearchPath(check.getAbsolutePath(), reservedSearchDirs)) {
            continue;
          } else {
            if (check.canRead() && !topDir.equals("/")) {
              searchFilesOfType(check.getAbsolutePath(), type, result, reservedSearchDirs);
            }
          }
        }
      }
    }

    return true;
  }
コード例 #2
0
  /*
   * kevin@xmic search file of specific type in a directory and return
   * sub-directories for caller to relay following searches make sure reserved
   * dirs added to sub-directories.
   */
  public ArrayList<String> relaySearchFiles(
      String topDir,
      String searchStr,
      ArrayList<String> typeList,
      ArrayList<String> accumulateResult,
      ArrayList<String> currentResult,
      ArrayList<String> reservedSearchDirs) {
    Log.i(TAG, "relaySearchFiles - topDir: " + topDir + " searchStr: " + searchStr);
    ArrayList<String> subDirs = new ArrayList<String>();

    File root_dir = new File(topDir);
    String[] list = root_dir.list();

    if (list != null && root_dir.canRead()) {
      int len = list.length;

      for (int i = 0; i < len; i++) {
        File check = new File(topDir + "/" + list[i]);

        // skip symbol link
        try {
          if (isSymlink(check)) {
            continue;
          }
        } catch (IOException e) {
          Log.e(TAG, "Failed to check symbol link!");
        }

        if (check.isFile()) {
          if (typeList != null) {
            String extension = DataUtil.getFileExtensionWithoutDot(check.getPath());
            if (typeList.contains(extension.toLowerCase())) {
              if (PATTERN_ALL.equals(searchStr)) {
                Log.i(TAG, "ADD result: " + check.getPath() + " as * match");
                accumulateResult.add(check.getPath());
                currentResult.add(check.getPath());
              } else {
                if (searchStr != null && check.getName().contains(searchStr)) {
                  accumulateResult.add(check.getPath());
                  currentResult.add(check.getPath());
                }
              }
            }
          } else {
            if (searchStr != null && check.getName().contains(searchStr)) {
              accumulateResult.add(check.getPath());
              currentResult.add(check.getPath());
            }
          }
        }
        if (check.isDirectory()) {
          if (StorageUtil.getExcludeSearchPath().contains(check.getAbsolutePath())
              || StorageUtil.isExcludeSearchPath(check.getAbsolutePath(), reservedSearchDirs)) {
            continue;
          } else {
            if (searchStr != null && check.getName().contains(searchStr)) {
              accumulateResult.add(check.getPath());
              currentResult.add(check.getPath());
            }
            if (check.canRead() && !topDir.equals("/")) {
              subDirs.add(check.getPath());
            }
          }
        }
      }
    }

    return subDirs;
  }