@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.line_list); // Setup the list adapter = new IconAdapter<FileWrapper>(this, R.layout.line_list_item); ListView list = (ListView) findViewById(R.id.list); list.setAdapter(adapter); list.setOnItemClickListener(this); // Load Directory if (savedInstanceState != null) { backStack = savedInstanceState.getParcelableArrayList("BACKSTACK"); } if (backStack == null || backStack.size() == 0) { backStack = new ArrayList<BackStackItem>(); String startPath = (startDirectory == null || startDirectory.isEmpty()) ? Environment.getExternalStorageDirectory().getPath() : startDirectory; backStack.add(new BackStackItem(startPath, false)); } wrapFiles(); this.setVolumeControlStream(AudioManager.STREAM_MUSIC); }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (backStack.size() > 1) { backStack.remove(backStack.size() - 1); wrapFiles(); } else { Intent intent = new Intent(); setResult(RESULT_CANCELED, intent); finish(); } return true; } return super.onKeyDown(keyCode, event); }
@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); } }
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(); }