Example usage for android.support.v4.provider DocumentFile listFiles

List of usage examples for android.support.v4.provider DocumentFile listFiles

Introduction

In this page you can find the example usage for android.support.v4.provider DocumentFile listFiles.

Prototype

public abstract DocumentFile[] listFiles();

Source Link

Usage

From source file:com.callrecorder.android.FileHelper.java

public static List<Recording> listRecordings(Context context) {
    final DocumentFile directory = getStorageFile(context);

    DocumentFile[] files = directory.listFiles();
    List<Recording> fileList = new ArrayList<>();
    for (DocumentFile file : files) {
        if (!file.getName().matches(Constants.FILE_NAME_PATTERN)) {
            Log.d(Constants.TAG, String.format("'%s' didn't match the file name pattern", file.getName()));
            continue;
        }//w  w  w.j av  a2s  .co m

        Recording recording = new Recording(file.getName());
        String phoneNum = recording.getPhoneNumber();
        recording.setUserName(getContactName(phoneNum, context));
        fileList.add(recording);
    }

    return fileList;
}

From source file:com.github.chenxiaolong.dualbootpatcher.switcher.InAppFlashingFragment.java

@Nullable
private static String[] getDirectories(Context context, Uri uri) {
    final ArrayList<String> filenames = new ArrayList<>();
    DocumentFile directory = FileUtils.getDocumentFile(context, uri);
    DocumentFile[] files = directory.listFiles();

    if (files == null) {
        return null;
    }//from  w  ww  .j ava2s  . c o m

    for (DocumentFile file : files) {
        if (file.isDirectory()) {
            filenames.add(file.getName());
        }
    }

    Collections.sort(filenames);

    return filenames.toArray(new String[filenames.size()]);
}

From source file:com.osama.cryptofm.tasks.DeleteTask.java

private void deleteDocFile(DocumentFile docFile) throws IOException {
    Log.d(TAG, "deleteDocFile: Deleting document file");
    publishProgress(docFile.getName());/*  www. j a  v  a  2s .  c o m*/
    if (docFile.isDirectory() && docFile.listFiles().length > 0) {
        for (DocumentFile d : docFile.listFiles()) {
            deleteDocFile(d);
        }
    }
    if (!docFile.delete()) {
        throw new IOException("cannot delete files");
    }
}

From source file:com.osama.cryptofm.tasks.EncryptTask.java

private void encryptDocumentFile(DocumentFile file) throws FileNotFoundException {
    Log.d(TAG, "encryptDocumentFile: encrypting document file");
    if (file.isDirectory()) {
        rootDocumentFile = file;// w ww.j  av a 2 s.c om
        for (DocumentFile f : file.listFiles()) {
            encryptDocumentFile(f);
        }
    } else {
        //check if root document is not null
        if (rootDocumentFile == null) {
            String path = mFilePaths.get(0).substring(0, mFilePaths.get(0).lastIndexOf('/'));

            Log.d(TAG, "encryptDocumentFile: Getting the root document: " + path);
            rootDocumentFile = FileDocumentUtils.getDocumentFile(new File(path));
        }
        DocumentFile temp = rootDocumentFile.createFile("pgp", file.getName() + ".pgp");
        mCreatedDocumentFiles.add(temp);
        publishProgress(file.getName(), "" + ((FileUtils.getReadableSize((file.length())))));

        InputStream in = CryptoFM.getContext().getContentResolver().openInputStream(file.getUri());
        OutputStream out = CryptoFM.getContext().getContentResolver().openOutputStream(temp.getUri());
        DocumentFileEncryption.encryptFile(in, out, pubKeyFile, true, new Date(file.lastModified()),
                file.getName());

    }
}

From source file:com.kanedias.vanilla.audiotag.PluginService.java

/**
 * 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 currentDir - document file representing current dir of search
 * @param remainingPathSegments - path segments that are left to find
 * @return URI for found file. Null if nothing found.
 *//*from   ww w  .  j a va 2 s.co  m*/
@Nullable
private Uri findInDocumentTree(DocumentFile currentDir, List<String> remainingPathSegments) {
    for (DocumentFile file : currentDir.listFiles()) {
        int index = remainingPathSegments.indexOf(file.getName());
        if (index == -1) {
            continue;
        }

        if (file.isDirectory()) {
            remainingPathSegments.remove(file.getName());
            return findInDocumentTree(file, remainingPathSegments);
        }

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

    return null;
}

From source file:com.osama.cryptofm.tasks.DecryptTask.java

private void decryptDocumentFiles(DocumentFile f) throws Exception {
    Log.d(TAG, "decryptDocumentFiles: Running decryption on document file");
    //first always check if task is canceled
    if (!isCancelled()) {
        if (f.isDirectory()) {
            for (DocumentFile tmpFile : f.listFiles()) {
                decryptDocumentFiles(tmpFile);
            }//  w w w.j  a  v  a  2s  .c om
        } else {
            File out = new File(rootPath + "/", f.getName().substring(0, f.getName().lastIndexOf('.')));
            // add the file in created files. top remove the files later of user cancels the task
            mCreatedFiles.add(out.getAbsoluteFile());
            publishProgress(f.getName(), "" + ((FileUtils.getReadableSize((f.length())))));

            DocumentFileEncryption.decryptFile(
                    CryptoFM.getContext().getContentResolver().openInputStream(f.getUri()), getSecretKey(),
                    mKeyPass, new BufferedOutputStream(new FileOutputStream(out))

            );

        }
    }
}

From source file:com.osama.cryptofm.tasks.DecryptTask.java

private ArrayList<String> getOnlyEncryptedDocumentFiles(ArrayList<String> files) {
    int size = files.size();
    for (int i = 0; i < size; i++) {
        DocumentFile file = FileDocumentUtils.getDocumentFile(new File(files.get(i)));
        //check if file is directory
        assert file != null;
        if (file.isDirectory()) {
            //get all the lists of files in directory
            ArrayList<String> tmp = new ArrayList<>();
            for (DocumentFile f : file.listFiles()) {
                tmp.add(FileUtils.CURRENT_PATH + f.getName());
            }//  ww w  . ja  va  2 s .  co  m
            //recursively get files
            getOnlyEncryptedDocumentFiles(tmp);
        }
        if (FileUtils.isEncryptedFile(mFilePaths.get(i))) {
            tmp.add(mFilePaths.get(i));
        }
    }
    //if there are no encrypted file
    if (tmp.size() < 1) {
        throw new IllegalArgumentException("No encrypted files found.");
    }
    return tmp;
}

From source file:org.dkf.jed2k.android.LollipopFileSystem.java

@Override
public File[] listFiles(File file, org.dkf.jed2k.android.FileFilter filter) {
    try {/*from   w ww .j a va 2 s .  c o m*/
        File[] files = file.listFiles(filter);
        if (files != null) {
            return files;
        }
    } catch (Exception e) {
        // ignore, try with SAF
    }

    LOG.warn("Using SAF for listFiles, could be a costly operation");

    DocumentFile f = getDirectory(app, file, false);
    if (f == null) {
        return null; // does not exists
    }

    DocumentFile[] files = f.listFiles();
    if (filter != null && files != null) {
        List<File> result = new ArrayList<>(files.length);
        for (int i = 0; i < files.length; i++) {
            Uri uri = files[i].getUri();
            String path = getDocumentPath(uri);
            if (path == null) {
                continue;
            }
            File child = new File(path);
            if (filter.accept(child)) {
                result.add(child);
            }
        }

        return result.toArray(new File[0]);
    }

    return new File[0];
}

From source file:com.frostwire.android.LollipopFileSystem.java

@Override
public File[] listFiles(File file, FileFilter filter) {
    try {/* w  w w  .  j  av a 2s. c o m*/
        File[] files = file.listFiles(filter);
        if (files != null) {
            return files;
        }
    } catch (Throwable e) {
        // ignore, try with SAF
    }

    LOG.warn("Using SAF for listFiles, could be a costly operation");

    DocumentFile f = getDirectory(app, file, false);
    if (f == null) {
        return null; // does not exists
    }

    DocumentFile[] files = f.listFiles();
    if (filter != null && files != null) {
        List<File> result = new ArrayList<>(files.length);
        for (DocumentFile file1 : files) {
            Uri uri = file1.getUri();
            String path = getDocumentPath(uri);
            if (path == null) {
                continue;
            }
            File child = new File(path);
            if (filter.accept(child)) {
                result.add(child);
            }
        }

        return result.toArray(new File[0]);
    }

    return new File[0];
}

From source file:com.osama.cryptofm.tasks.MoveTask.java

private void moveDocumentFile(DocumentFile file) throws Exception {
    if (file.isDirectory()) {
        Log.d(TAG, "moveDocumentFile: Yes document file is directory");
        //change the destination folder
        mDestinationFolder = mDestinationFolder + file.getName() + "/";
        //check if destination folder is not document file
        if (FileUtils.isDocumentFile(mDestinationFolder)) {
            rootDocumentFile = rootDocumentFile.createDirectory(file.getName());
        } else {/*from   w  ww  . j a va 2 s . c om*/
            File tmp = new File(mDestinationFolder);
            if (!tmp.exists()) {
                tmp.mkdir();
            } else {
                return;
            }
        }
        for (DocumentFile f : file.listFiles()) {
            moveDocumentFile(f);
        }
    } else {
        isNextFile = true;
        publishProgress(file.getName());
        publishProgress("" + 0);
        //check if pasting in internal storage
        if (!FileUtils.isDocumentFile(mDestinationFolder)) {
            Log.d(TAG, "moveDocumentFile: moving document file in internal storage");
            File destinationFile = new File(mDestinationFolder + file.getName());
            innerMove(CryptoFM.getContext().getContentResolver().openInputStream(file.getUri()),
                    new BufferedOutputStream(new FileOutputStream(destinationFile)), file.length());
        } else {
            Log.d(TAG, "moveDocumentFile: Moving document file honey");
            DocumentFile destFile = rootDocumentFile.createFile(file.getType(), file.getName());

            innerMove(CryptoFM.getContext().getContentResolver().openInputStream(file.getUri()),
                    CryptoFM.getContext().getContentResolver().openOutputStream(destFile.getUri()),
                    file.length());
        }
    }

    //delete the input file
    //if copying then don't
    if (SharedData.IS_COPYING_NOT_MOVING) {
        return;
    }
    if (!file.delete()) {
        throw new IOException("cannot move files");
    }
}