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;
}
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;
}
Aggregations