Java Code Examples for androidx.documentfile.provider.DocumentFile#isDirectory()

The following examples show how to use androidx.documentfile.provider.DocumentFile#isDirectory() . 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: FetchHelper.java    From Aria2App with GNU General Public License v3.0 6 votes vote down vote up
@NonNull
private static DocumentFile createAllDirs(@NonNull DocumentFile parent, @NonNull String filePath) throws PreparationException {
    String[] split = filePath.split(Pattern.quote(File.separator));
    for (int i = 0; i < split.length - 1; i++) { // Skip last segment
        DocumentFile doc = parent.findFile(split[i]);
        if (doc == null || !doc.isDirectory()) {
            parent = parent.createDirectory(split[i]);
            if (parent == null)
                throw new PreparationException("Couldn't create directory: " + split[i]);
        } else {
            parent = doc;
        }
    }

    return parent;
}
 
Example 2
Source File: LocalBackupStorage.java    From SAI with GNU General Public License v3.0 6 votes vote down vote up
@Override
public List<Uri> listBackupFiles() {
    List<Uri> uris = new ArrayList<>();

    DocumentFile backupsDir = SafUtils.docFileFromTreeUriOrFileUri(mContext, getBackupDirUriOrThrow());
    if (backupsDir == null)
        return uris;

    for (DocumentFile docFile : backupsDir.listFiles()) {
        if (docFile.isDirectory())
            continue;

        String docName = docFile.getName();
        if (docName == null)
            continue;

        String docExt = Utils.getExtension(docName);
        if (docExt == null || !docExt.toLowerCase().equals("apks"))
            continue;

        uris.add(namespaceUri(docFile.getUri()));
    }

    return uris;
}
 
Example 3
Source File: SAFUtil.java    From VinylMusicPlayer with GNU General Public License v3.0 6 votes vote down vote up
/**
 * https://github.com/vanilla-music/vanilla-music-tag-editor/commit/e00e87fef289f463b6682674aa54be834179ccf0#diff-d436417358d5dfbb06846746d43c47a5R359
 * Finds needed file through Document API for SAF. It's not optimized yet - you can still gain wrong URI on
 * files such as "/a/b/c.mp3" and "/b/a/c.mp3", but I consider it complete enough to be usable.
 *
 * @param dir      - document file representing current dir of search
 * @param segments - path segments that are left to find
 * @return URI for found file. Null if nothing found.
 */
@Nullable
public static Uri findDocument(DocumentFile dir, List<String> segments) {
    for (DocumentFile file : dir.listFiles()) {
        int index = segments.indexOf(file.getName());
        if (index == -1) {
            continue;
        }

        if (file.isDirectory()) {
            segments.remove(file.getName());
            return findDocument(file, segments);
        }

        if (file.isFile() && index == segments.size() - 1) {
            // got to the last part
            return file.getUri();
        }
    }

    return null;
}
 
Example 4
Source File: SAFUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 指定したディレクトリ配下に存在するファイルの一覧を取得
 * @param dir
 * @param filter nullなら存在するファイルを全て追加
 * @return
 */
@NonNull
public static List<DocumentFile> listFiles(
	@NonNull final DocumentFile dir,
	@Nullable final FileFilter filter) {

	final List<DocumentFile> result = new ArrayList<DocumentFile>();
	if (dir.isDirectory()) {
		final DocumentFile[] files = dir.listFiles();
		for (final DocumentFile file: files) {
			if ((filter == null) || (filter.accept(file))) {
				result.add(file);
			}
		}
	}
	return result;
}
 
Example 5
Source File: DocumentTreeRecyclerAdapter.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * コンストラクタ
 * @param context
 * @param layoutResId
 * @param root ディレクトリを示すDocumentFile
 */
public DocumentTreeRecyclerAdapter(
	@NonNull final Context context,
	@LayoutRes final int layoutResId,
	@NonNull final DocumentFile root) {

	super();
	if (root == null || !root.isDirectory()) {
		throw new IllegalArgumentException("root should be a directory!");
	}
	mContext = context;
	mLayoutRes = layoutResId;
	mRoot = root;
	mAsyncHandler = HandlerThreadHandler.createHandler(TAG);
	internalChangeDir(root);
}
 
Example 6
Source File: DocumentTreeRecyclerAdapter.java    From libcommon with Apache License 2.0 6 votes vote down vote up
@Override
public boolean accept(@NonNull final DocumentFile file) {
	if (mExtFilter == null) {
		return true;	// フィルタ無しのときはtrue
	}
	if (file.isDirectory()) {
		return true;	// ディレクトリのときは、true
	}
	final String name = file.getName().toLowerCase(Locale.getDefault());
	for (final String ext : mExtFilter) {
		if (name.endsWith("." + ext)) {
			return true;	// 拡張子が一致すればtrue
		}
	}
	return false;
}
 
Example 7
Source File: SAFUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 指定したディレクトリ配下に存在するファイルの一覧を取得
 * @param dir
 * @param filter nullなら存在するファイルを全て追加
 * @return
 */
@NonNull
public static List<DocumentFile> listFiles(
	@NonNull final DocumentFile dir,
	@Nullable final FileFilter filter) {

	final List<DocumentFile> result = new ArrayList<DocumentFile>();
	if (dir.isDirectory()) {
		final DocumentFile[] files = dir.listFiles();
		for (final DocumentFile file: files) {
			if ((filter == null) || (filter.accept(file))) {
				result.add(file);
			}
		}
	}
	return result;
}
 
Example 8
Source File: FileHelper.java    From Hentoid with Apache License 2.0 6 votes vote down vote up
public static boolean checkAndSetRootFolder(@NonNull final Context context, @NonNull final DocumentFile folder, boolean notify) {
    // Validate folder
    if (!folder.exists() && !folder.isDirectory()) {
        if (notify)
            ToastUtil.toast(context, R.string.error_creating_folder);
        return false;
    }

    // Remove and add back the nomedia file to test if the user has the I/O rights to the selected folder
    DocumentFile nomedia = findFile(context, folder, NOMEDIA_FILE_NAME);
    if (nomedia != null) nomedia.delete();

    nomedia = folder.createFile("application/octet-steam", NOMEDIA_FILE_NAME);
    if (null != nomedia && nomedia.exists()) {
        boolean deleted = nomedia.delete();
        if (deleted) Timber.d(".nomedia file deleted");
    } else {
        if (notify)
            ToastUtil.toast(context, R.string.error_write_permission);
        return false;
    }

    Preferences.setStorageUri(folder.getUri().toString());
    return true;
}
 
Example 9
Source File: SAFUtils.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * 指定したDocumentFileが書き込み可能であればその下にディレクトリを生成して
 * そのディレクトリを示すDocumentFileオブジェクトを返す
 * @param parent
 * @param dirs
 * @return
 * @throws IOException
 */
@NonNull
public static DocumentFile getDir(
	@NonNull final DocumentFile parent, @Nullable final String dirs)
		throws IOException {
	
	DocumentFile tree = parent;
	if (!TextUtils.isEmpty(dirs)) {
		final String[] dir = dirs.split("/");
		for (final String d: dir) {
			if (!TextUtils.isEmpty(d)) {
				final DocumentFile t = tree.findFile(d);
				if ((t != null) && t.isDirectory()) {
					// 既に存在している時は何もしない
					tree = t;
				} else if (t == null) {
					if (tree.canWrite()) {
						// 存在しないときはディレクトリを生成
						tree = tree.createDirectory(d);
					} else {
						throw new IOException("can't create directory");
					}
				} else {
					throw new IOException("can't create directory, file with same name already exists");
				}
			}
		}
	}
	return tree;
}
 
Example 10
Source File: SAFUtils.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * 指定したDocumentFileが指し示すフォルダの下に指定した相対パスのディレクトリ階層を生成する
 * フォルダが存在していない時に書き込み可能でなければIOExceptionを投げる
 * deprecated #getDirを使うこと
 * @param context
 * @param baseDoc
 * @param dirs
 * @return
 * @throws IOException
 */
@Deprecated
@NonNull
public static DocumentFile getDocumentFile(
	@NonNull Context context,
	@NonNull final DocumentFile baseDoc, @Nullable final String dirs)
		throws IOException {
	
	DocumentFile tree = baseDoc;
	if (!TextUtils.isEmpty(dirs)) {
		final String[] dir = dirs.split("/");
		for (final String d: dir) {
			if (!TextUtils.isEmpty(d)) {
				final DocumentFile t = tree.findFile(d);
				if ((t != null) && t.isDirectory()) {
					// 既に存在している時は何もしない
					tree = t;
				} else if (t == null) {
					if (tree.canWrite()) {
						// 存在しないときはディレクトリを生成
						tree = tree.createDirectory(d);
					} else {
						throw new IOException("can't create directory");
					}
				} else {
					throw new IOException("can't create directory, file with same name already exists");
				}
			}
		}
	}
	return tree;
}
 
Example 11
Source File: DocumentTreeRecyclerAdapter.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * 指定したディレクトリへ切り替え
 * コンストラクタのroot引数で指定したディレクトリorその下位ディレクトリを指定すること
 * @param newDir
 * @throws IOException
 */
public void changeDir(@NonNull final DocumentFile newDir) throws IOException {
	if (DEBUG) Log.v(TAG, "changeDir:newDir=" + newDir.getUri());
	if (newDir.isDirectory()) {
		internalChangeDir(newDir);
		notifyDataSetChanged();
	} else {
		throw new IOException(String.format(Locale.US, "%s is not a directory/could not read",
			newDir.getUri()));
	}
}
 
Example 12
Source File: SAFUtils.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * 指定したDocumentFileが書き込み可能であればその下にディレクトリを生成して
 * そのディレクトリを示すDocumentFileオブジェクトを返す
 * @param parent
 * @param dirs
 * @return
 * @throws IOException
 */
@NonNull
public static DocumentFile getDir(
	@NonNull final DocumentFile parent, @Nullable final String dirs)
		throws IOException {
	
	DocumentFile tree = parent;
	if (!TextUtils.isEmpty(dirs)) {
		final String[] dir = dirs.split("/");
		for (final String d: dir) {
			if (!TextUtils.isEmpty(d)) {
				if ("..".equals(d)) {
					tree = tree.getParentFile();
					if (tree == null) {
						throw new IOException("failed to get parent directory");
					}
				} else if (!".".equals(d)) {
					final DocumentFile t = tree.findFile(d);
					if ((t != null) && t.isDirectory()) {
						// 既に存在している時は何もしない
						tree = t;
					} else if (t == null) {
						if (tree.canWrite()) {
							// 存在しないときはディレクトリを生成
							tree = tree.createDirectory(d);
						} else {
							throw new IOException("can't create directory");
						}
					} else {
						throw new IOException("can't create directory, file with same name already exists");
					}
				}
			}
		}
	}
	return tree;
}
 
Example 13
Source File: ImportHelper.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
public static DocumentFile getExistingHentoidDirFrom(@NonNull final Context context, @NonNull final DocumentFile root) {
    if (!root.exists() || !root.isDirectory() || null == root.getName()) return root;

    // Selected folder _is_ the Hentoid folder
    if (isHentoidFolderName(root.getName())) return root;

    // If not, look for it in its children
    List<DocumentFile> hentoidDirs = FileHelper.listFoldersFilter(context, root, hentoidFolderNames);
    if (!hentoidDirs.isEmpty()) return hentoidDirs.get(0);
    else return root;
}