/* Scan the files in the new directory, and store them in the filelist. * Update the UI by refreshing the list adapter. */ private void loadDirectory(String newdirectory) { if (newdirectory.equals("../")) { try { directory = new File(directory).getParent(); } catch (Exception e) { } } else { directory = newdirectory; } SharedPreferences.Editor editor = getPreferences(0).edit(); editor.putString("lastBrowsedDirectory", directory); editor.commit(); directoryView.setText(directory); filelist = new ArrayList<FileUri>(); ArrayList<FileUri> sortedDirs = new ArrayList<FileUri>(); ArrayList<FileUri> sortedFiles = new ArrayList<FileUri>(); if (!newdirectory.equals(rootdir)) { String parentDirectory = new File(directory).getParent() + "/"; Uri uri = Uri.parse("file://" + parentDirectory); sortedDirs.add(new FileUri(uri, parentDirectory)); } try { File dir = new File(directory); File[] files = dir.listFiles(); if (files != null) { for (File file : files) { if (file == null) { continue; } String filename = file.getName(); if (file.isDirectory()) { Uri uri = Uri.parse("file://" + file.getAbsolutePath() + "/"); FileUri fileuri = new FileUri(uri, uri.getPath()); sortedDirs.add(fileuri); } else if (filename.endsWith(".mid") || filename.endsWith(".MID") || filename.endsWith(".midi") || filename.endsWith(".MIDI")) { Uri uri = Uri.parse("file://" + file.getAbsolutePath()); FileUri fileuri = new FileUri(uri, uri.getLastPathSegment()); sortedFiles.add(fileuri); } } } } catch (Exception e) { } if (sortedDirs.size() > 0) { Collections.sort(sortedDirs, sortedDirs.get(0)); } if (sortedFiles.size() > 0) { Collections.sort(sortedFiles, sortedFiles.get(0)); } filelist.addAll(sortedDirs); filelist.addAll(sortedFiles); adapter = new IconArrayAdapter<FileUri>(this, android.R.layout.simple_list_item_1, filelist); this.setListAdapter(adapter); }
public FileWrapper(File aFile, int type, boolean aIsEnabled) { file = aFile; parentItem = type == PARENT; dirSelectItem = type == DIRSELECT; typeIndex = type == FILE ? (FILE + (file.isDirectory() ? 0 : 1)) : type; enabled = parentItem || dirSelectItem || aIsEnabled; }
private void wrapFiles() { listedDirectory = new File(backStack.get(backStack.size() - 1).path); if (!listedDirectory.isDirectory()) { throw new IllegalArgumentException("Directory is not valid."); } adapter.clear(); setTitle(listedDirectory.getAbsolutePath()); if (isDirectoryTarget) adapter.add(new FileWrapper(null, FileWrapper.DIRSELECT, true)); if (listedDirectory.getParentFile() != null) adapter.add(new FileWrapper(null, FileWrapper.PARENT, true)); // Copy new items final File[] files = listedDirectory.listFiles(); if (files != null) { for (File file : files) { String path = file.getName(); boolean allowFile = file.isDirectory() || (filterPath(path) && !isDirectoryTarget); if (allowFile) adapter.add(new FileWrapper(file, FileWrapper.FILE, file.isDirectory() || true)); } } // Sort items adapter.sort( new Comparator<FileWrapper>() { @Override public int compare(FileWrapper aLeft, FileWrapper aRight) { return aLeft.compareTo(aRight); }; }); // Update adapter.notifyDataSetChanged(); }
@Override public void onItemClick(AdapterView<?> aListView, View aView, int aPosition, long aID) { final FileWrapper item = adapter.getItem(aPosition); if (item.parentItem && backStack.get(backStack.size() - 1).parentIsBack) { backStack.remove(backStack.size() - 1); wrapFiles(); return; } else if (item.dirSelectItem) { finishWithPath(listedDirectory.getAbsolutePath()); return; } final File selected = item.parentItem ? listedDirectory.getParentFile() : item.file; if (selected.isDirectory()) { backStack.add(new BackStackItem(selected.getAbsolutePath(), !item.parentItem)); wrapFiles(); } else { String filePath = selected.getAbsolutePath(); finishWithPath(filePath); } }