public void onClick(FileItem source) { // Verify if the item is a folder. File file = source.getFile(); if (file.isDirectory()) { // Open the folder. FileChooserCore.this.loadFolder(file); } else { // Notify the listeners. FileChooserCore.this.notifyListeners(file, null); } }
/** * Loads all the files of a folder in the file chooser. * * <p>If no path is specified ('folder' is null) the root folder of the SD card is going to be * used. * * @param folder The folder. */ public void loadFolder(File folder) { // Remove previous files. LinearLayout root = this.chooser.getRootLayout(); LinearLayout layout = (LinearLayout) root.findViewById(R.id.linearLayoutFiles); layout.removeAllViews(); // Get the file path. if (folder == null || !folder.exists()) { if (defaultFolder != null) { this.currentFolder = defaultFolder; } else { this.currentFolder = Environment.getExternalStorageDirectory(); } } else { this.currentFolder = folder; } // Verify if the path exists. if (this.currentFolder.exists() && layout != null) { List<FileItem> fileItems = new LinkedList<FileItem>(); // Add the parent folder. if (this.currentFolder.getParent() != null) { File parent = new File(this.currentFolder.getParent()); if (parent.exists()) { fileItems.add(new FileItem(this.chooser.getContext(), parent, "..")); } } // Verify if the file is a directory. if (this.currentFolder.isDirectory()) { // Get the folder's files. File[] fileList = this.currentFolder.listFiles(); if (fileList != null) { // Order the files alphabetically and separating folders from files. Arrays.sort( fileList, new Comparator<File>() { public int compare(File file1, File file2) { if (file1 != null && file2 != null) { if (file1.isDirectory() && (!file2.isDirectory())) return -1; if (file2.isDirectory() && (!file1.isDirectory())) return 1; return file1.getName().compareTo(file2.getName()); } return 0; } }); // Iterate all the files in the folder. for (int i = 0; i < fileList.length; i++) { // Verify if file can be selected (is a directory or folder mode is not activated and // the file pass the filter, if defined). boolean selectable = true; if (!fileList[i].isDirectory()) { selectable = !this.folderMode && (this.filter == null || fileList[i].getName().matches(this.filter)); } // Verify if the file must be show. if (selectable || !this.showOnlySelectable) { // Create the file item and add it to the list. FileItem fileItem = new FileItem(this.chooser.getContext(), fileList[i]); fileItem.setSelectable(selectable); fileItems.add(fileItem); } } } // Set the name of the current folder. String currentFolderName = this.showFullPathInTitle ? this.currentFolder.getPath() : this.currentFolder.getName(); this.chooser.setCurrentFolderName(currentFolderName); } else { // The file is not a folder, add only this file. fileItems.add(new FileItem(this.chooser.getContext(), this.currentFolder)); } // Add click listener and add the FileItem objects to the layout. for (int i = 0; i < fileItems.size(); i++) { fileItems.get(i).addListener(this.fileItemClickListener); layout.addView(fileItems.get(i)); } // Refresh default folder. defaultFolder = this.currentFolder; } }