Search in sources :

Example 1 with FileObject

use of com.simplecity.amp_library.model.FileObject in project Shuttle by timusus.

the class FileBrowser method loadDir.

/**
 * Loads the specified folder.
 *
 * @param directory The file object to points to the directory to load.
 * @return An {@link List<BaseFileObject>} object that holds the data of the specified directory.
 */
@WorkerThread
public List<BaseFileObject> loadDir(File directory) {
    ThreadUtils.ensureNotOnMainThread();
    currentDir = directory;
    List<BaseFileObject> folderObjects = new ArrayList<>();
    List<BaseFileObject> fileObjects = new ArrayList<>();
    // Grab a list of all files/subdirs within the specified directory.
    File[] files = directory.listFiles(FileHelper.getAudioFilter());
    if (files != null) {
        for (File file : files) {
            BaseFileObject baseFileObject;
            if (file.isDirectory()) {
                baseFileObject = new FolderObject();
                baseFileObject.path = FileHelper.getPath(file);
                baseFileObject.name = file.getName();
                File[] listOfFiles = file.listFiles(FileHelper.getAudioFilter());
                if (listOfFiles != null && listOfFiles.length > 0) {
                    for (File listOfFile : listOfFiles) {
                        if (listOfFile.isDirectory()) {
                            ((FolderObject) baseFileObject).folderCount++;
                        } else {
                            ((FolderObject) baseFileObject).fileCount++;
                        }
                    }
                } else {
                    continue;
                }
                if (!folderObjects.contains(baseFileObject)) {
                    folderObjects.add(baseFileObject);
                }
            } else {
                baseFileObject = new FileObject();
                baseFileObject.path = FileHelper.getPath(file);
                baseFileObject.name = FileHelper.getName(file.getName());
                baseFileObject.size = file.length();
                ((FileObject) baseFileObject).extension = FileHelper.getExtension(file.getName());
                if (TextUtils.isEmpty(((FileObject) baseFileObject).extension)) {
                    continue;
                }
                ((FileObject) baseFileObject).tagInfo = new TagInfo(baseFileObject.path);
                if (!fileObjects.contains(baseFileObject)) {
                    fileObjects.add(baseFileObject);
                }
            }
        }
    }
    sortFileObjects(fileObjects);
    sortFolderObjects(folderObjects);
    if (!settingsManager.getFolderBrowserFilesAscending()) {
        Collections.reverse(fileObjects);
    }
    if (!settingsManager.getFolderBrowserFoldersAscending()) {
        Collections.reverse(folderObjects);
    }
    folderObjects.addAll(fileObjects);
    if (!FileHelper.isRootDirectory(currentDir)) {
        FolderObject parentObject = new FolderObject();
        parentObject.fileType = FileType.PARENT;
        parentObject.name = FileHelper.PARENT_DIRECTORY;
        parentObject.path = FileHelper.getPath(currentDir) + "/" + FileHelper.PARENT_DIRECTORY;
        folderObjects.add(0, parentObject);
    }
    return folderObjects;
}
Also used : BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) TagInfo(com.simplecity.amp_library.model.TagInfo) ArrayList(java.util.ArrayList) FolderObject(com.simplecity.amp_library.model.FolderObject) FileObject(com.simplecity.amp_library.model.FileObject) BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) File(java.io.File) WorkerThread(android.support.annotation.WorkerThread)

Example 2 with FileObject

use of com.simplecity.amp_library.model.FileObject in project Shuttle by timusus.

the class FileHelper method renameFile.

/**
 * Renames an {@link FileObject} to the passed in newName
 *
 * @param context Context
 * @param baseFileObject the FileObject representation of the file to rename
 * @param newName the new name of the file
 */
public static boolean renameFile(Context context, BaseFileObject baseFileObject, String newName) {
    if (newName == null) {
        return false;
    }
    if (baseFileObject instanceof FileObject) {
        String ext = ((FileObject) baseFileObject).extension;
        if (ext == null) {
            ext = "";
        }
        newName = newName + "." + ext;
    }
    File file = new File(baseFileObject.path);
    File newFile = new File(baseFileObject.getParent(), newName);
    if (file.renameTo(newFile)) {
        baseFileObject.name = FileHelper.getName(newFile.getName());
        CustomMediaScanner.scanFiles(context, Collections.singletonList(file.getPath()), null);
        return true;
    }
    return false;
}
Also used : FileObject(com.simplecity.amp_library.model.FileObject) BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) File(java.io.File)

Aggregations

BaseFileObject (com.simplecity.amp_library.model.BaseFileObject)2 FileObject (com.simplecity.amp_library.model.FileObject)2 File (java.io.File)2 WorkerThread (android.support.annotation.WorkerThread)1 FolderObject (com.simplecity.amp_library.model.FolderObject)1 TagInfo (com.simplecity.amp_library.model.TagInfo)1 ArrayList (java.util.ArrayList)1