Search in sources :

Example 1 with WritablePathObject

use of com.genonbeta.TrebleShot.object.WritablePathObject in project TrebleShot by genonbeta.

the class FileListAdapter method onLoad.

@Override
public ArrayList<GenericFileHolder> onLoad() {
    DocumentFile path = getPath();
    ArrayList<GenericFileHolder> list = new ArrayList<>();
    ArrayList<GenericFileHolder> folders = new ArrayList<>();
    ArrayList<GenericFileHolder> files = new ArrayList<>();
    if (path != null) {
        DocumentFile[] fileIndex = path.listFiles();
        if (fileIndex != null && fileIndex.length > 0) {
            for (DocumentFile file : fileIndex) {
                if ((mFileMatch != null && !file.getName().matches(mFileMatch)))
                    continue;
                if (file.isDirectory() && mShowDirectories)
                    folders.add(new DirectoryHolder(file, mContext.getString(R.string.text_folder), R.drawable.ic_folder_white_24dp));
                else if (file.isFile() && mShowFiles)
                    files.add(new FileHolder(getContext(), file));
            }
            Collections.sort(folders, getDefaultComparator());
            Collections.sort(files, getDefaultComparator());
        }
    } else {
        ArrayList<File> referencedDirectoryList = new ArrayList<>();
        DocumentFile defaultFolder = FileUtils.getApplicationDirectory(getContext());
        folders.add(new DirectoryHolder(defaultFolder, getContext().getString(R.string.text_receivedFiles), R.drawable.ic_whatshot_white_24dp));
        folders.add(new DirectoryHolder(DocumentFile.fromFile(new File(".")), mContext.getString(R.string.text_fileRoot), mContext.getString(R.string.text_folder), R.drawable.ic_folder_white_24dp));
        if (Build.VERSION.SDK_INT >= 21)
            referencedDirectoryList.addAll(Arrays.asList(getContext().getExternalMediaDirs()));
        else if (Build.VERSION.SDK_INT >= 19)
            referencedDirectoryList.addAll(Arrays.asList(getContext().getExternalFilesDirs(null)));
        else
            referencedDirectoryList.add(Environment.getExternalStorageDirectory());
        for (File mediaDir : referencedDirectoryList) {
            if (mediaDir == null || !mediaDir.canWrite())
                continue;
            DirectoryHolder fileHolder = new DirectoryHolder(DocumentFile.fromFile(mediaDir), getContext().getString(R.string.text_storage), R.drawable.ic_save_white_24dp);
            String[] splitPath = mediaDir.getAbsolutePath().split(File.separator);
            if (splitPath.length >= 2 && splitPath[1].equals("storage")) {
                if (splitPath.length >= 4 && splitPath[2].equals("emulated")) {
                    File file = new File(buildPath(splitPath, 4));
                    if (file.canWrite()) {
                        fileHolder.file = DocumentFile.fromFile(file);
                        fileHolder.friendlyName = "0".equals(splitPath[3]) ? getContext().getString(R.string.text_internalStorage) : getContext().getString(R.string.text_emulatedMediaDirectory, splitPath[3]);
                    }
                } else if (splitPath.length >= 3) {
                    File file = new File(buildPath(splitPath, 3));
                    if (!file.canWrite())
                        continue;
                    fileHolder.friendlyName = splitPath[2];
                    fileHolder.file = DocumentFile.fromFile(file);
                }
            }
            folders.add(fileHolder);
        }
        ArrayList<WritablePathObject> objectList = getDatabase().castQuery(new SQLQuery.Select(AccessDatabase.TABLE_WRITABLEPATH), WritablePathObject.class);
        if (Build.VERSION.SDK_INT >= 21) {
            for (WritablePathObject pathObject : objectList) try {
                folders.add(new WritablePathHolder(DocumentFile.fromUri(getContext(), pathObject.path, true), pathObject, getContext().getString(R.string.text_storage)));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    list.addAll(folders);
    list.addAll(files);
    return list;
}
Also used : DocumentFile(com.genonbeta.TrebleShot.io.DocumentFile) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) WritablePathObject(com.genonbeta.TrebleShot.object.WritablePathObject) SQLQuery(com.genonbeta.android.database.SQLQuery) File(java.io.File) DocumentFile(com.genonbeta.TrebleShot.io.DocumentFile)

Aggregations

DocumentFile (com.genonbeta.TrebleShot.io.DocumentFile)1 WritablePathObject (com.genonbeta.TrebleShot.object.WritablePathObject)1 SQLQuery (com.genonbeta.android.database.SQLQuery)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1