Java Code Examples for android.support.v4.provider.DocumentFile#listFiles()

The following examples show how to use android.support.v4.provider.DocumentFile#listFiles() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TGSafBrowser.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void listElements(TGBrowserCallBack<List<TGBrowserElement>> cb) {
	try {
		List<TGBrowserElement> elements = new ArrayList<TGBrowserElement>();
		if( this.element != null ) {
			DocumentFile file = this.element.getFile();
			if( file.exists() && file.isDirectory() ) {
				DocumentFile[] children = file.listFiles();
				if( children != null ) {
					for(DocumentFile child : children){
						elements.add(new TGSafBrowserElement(child, this.element));
					}
				}
			}
			if( !elements.isEmpty() ){
				Collections.sort(elements, new TGBrowserElementComparator());
			}
		}

		cb.onSuccess(elements);
	} catch (Throwable e) {
		cb.handleError(e);
	}
}
 
Example 2
Source File: TreeUriScannerIntentService.java    From fdroidclient with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Recursively search for {@link IndexV1Updater#SIGNED_FILE_NAME} starting
 * from the given directory, looking at files first before recursing into
 * directories.  This is "depth last" since the index file is much more
 * likely to be shallow than deep, and there can be a lot of files to
 * search through starting at 4 or more levels deep, like the fdroid
 * icons dirs and the per-app "external storage" dirs.
 */
private void searchDirectory(DocumentFile documentFileDir) {
    DocumentFile[] documentFiles = documentFileDir.listFiles();
    if (documentFiles == null) {
        return;
    }
    boolean foundIndex = false;
    ArrayList<DocumentFile> dirs = new ArrayList<>();
    for (DocumentFile documentFile : documentFiles) {
        if (documentFile.isDirectory()) {
            dirs.add(documentFile);
        } else if (!foundIndex) {
            if (IndexV1Updater.SIGNED_FILE_NAME.equals(documentFile.getName())) {
                registerRepo(documentFile);
                foundIndex = true;
            }
        }
    }
    for (DocumentFile dir : dirs) {
        searchDirectory(dir);
    }
}
 
Example 3
Source File: FileUtils.java    From Rey-MusicPlayer with Apache License 2.0 5 votes vote down vote up
private static boolean deleteDirectory(DocumentFile documentFile) {
    DocumentFile files[] = documentFile.listFiles();
    for (DocumentFile file : files) {
        Logger.log("File Name :-  " + file.getName());
        if (file.isDirectory()) {
            deleteDirectory(file);
        } else {
            return file.delete();
        }
    }
    return false;
}
 
Example 4
Source File: OTGUtil.java    From PowerFileExplorer with GNU General Public License v3.0 4 votes vote down vote up
/**
   * Returns an array of list of files at a specific path in OTG
   *
   * @param path    the path to the directory tree, starts with prefix 'otg:/'
   *                Independent of URI (or mount point) for the OTG
   * @param context context for loading
   * @return an array of list of files at the path
   */
  public static ArrayList<BaseFile> getDocumentFilesList(final String path, final Context context, final ContentFragment.LoadFiles updater) {
      final SharedPreferences manager = PreferenceManager.getDefaultSharedPreferences(context);
      final String rootUriString = manager.getString(ExplorerActivity.KEY_PREF_OTG, null);
      DocumentFile rootUri = DocumentFile.fromTreeUri(context, Uri.parse(rootUriString));
      ArrayList<BaseFile> files = new ArrayList<>(1024);

      final String[] parts = path.split("/");
      for (int i = 0; i < parts.length; i++) {

          // first omit 'otg:/' before iterating through DocumentFile
          if (path.equals(OTGUtil.PREFIX_OTG + "/")) break;
          if (parts[i].equals("otg:") || parts[i].equals("")) continue;
          Log.d(context.getClass().getSimpleName(), "Currently at: " + parts[i]);
          // iterating through the required path to find the end point
          rootUri = rootUri.findFile(parts[i]);
      }

long prevUpdate = System.currentTimeMillis();
Log.d(context.getClass().getSimpleName(), "Found URI for: " + rootUri.getName());
      // we have the end point DocumentFile, list the files inside it and return
      for (DocumentFile file : rootUri.listFiles()) {
          try {
              if (file.exists()) {
                  long size = 0;
                  if (!file.isDirectory()) size = file.length();
                  Log.d(context.getClass().getSimpleName(), "Found file: " + file.getName());
                  BaseFile baseFile = new BaseFile(path + "/" + file.getName(),
                          RootHelper.parseDocumentFilePermission(file), file.lastModified(), size, file.isDirectory());
                  baseFile.setName(file.getName());
                  baseFile.setMode(OpenMode.OTG);
                  files.add(baseFile);
			final long present = System.currentTimeMillis();
			if (updater != null && present - prevUpdate > 1000 && !updater.busyNoti) {
				prevUpdate = present;
				updater.publish(files);
				files = new ArrayList<>(1024);
			}
              }
          } catch (Exception e) {
          }
      }
if (updater != null) {
	updater.publish(files);
}
      return files;
  }