use of group.pals.android.lib.ui.filechooser.io.IFileFilter in project FBReaderJ by geometer.
the class FileChooserActivity method setLocation.
// setLocation()
/**
* Sets current location.
*
* @param path
* the path
* @param listener
* {@link TaskListener}: the second parameter {@code any} in
* {@link TaskListener#onFinish(boolean, Object)} will be
* {@code path}.
* @param selectedFile
* the file should be selected after loading location done. Can
* be {@code null}.
*/
private void setLocation(final IFile path, final TaskListener listener, final IFile selectedFile) {
new LoadingDialog(this, R.string.afc_msg_loading, true) {
// IFile[] files = new IFile[0];
List<IFile> files;
boolean hasMoreFiles = false;
int shouldBeSelectedIdx = -1;
/**
* Used to focus last directory on list view.
*/
String mLastPath = getLocation() != null ? getLocation().getAbsolutePath() : null;
@Override
protected Object doInBackground(Void... params) {
try {
if (path.isDirectory() && path.canRead()) {
files = new ArrayList<IFile>();
mFileProvider.listAllFiles(path, new IFileFilter() {
@Override
public boolean accept(IFile pathname) {
if (mFileProvider.accept(pathname)) {
if (files.size() < mFileProvider.getMaxFileCount())
files.add(pathname);
else
hasMoreFiles = true;
}
return false;
}
});
} else
files = null;
if (files != null) {
Collections.sort(files, new FileComparator(mFileProvider.getSortType(), mFileProvider.getSortOrder()));
if (selectedFile != null && selectedFile.exists() && selectedFile.parentFile().equalsToPath(path)) {
for (int i = 0; i < files.size(); i++) {
if (files.get(i).equalsToPath(selectedFile)) {
shouldBeSelectedIdx = i;
break;
}
}
} else if (mLastPath != null && mLastPath.length() >= path.getAbsolutePath().length()) {
for (int i = 0; i < files.size(); i++) {
IFile f = files.get(i);
if (f.isDirectory() && mLastPath.startsWith(f.getAbsolutePath())) {
shouldBeSelectedIdx = i;
break;
}
}
}
}
// if files != null
} catch (Throwable t) {
setLastException(t);
cancel(false);
}
return null;
}
// doInBackground()
@Override
protected void onCancelled() {
super.onCancelled();
Dlg.toast(FileChooserActivity.this, R.string.afc_msg_cancelled, Dlg._LengthShort);
}
// onCancelled()
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
if (files == null) {
Dlg.toast(FileChooserActivity.this, mTextResources.get("permissionDenied"), Dlg._LengthShort);
if (listener != null)
listener.onFinish(false, path);
return;
}
// update list view
createIFileAdapter();
for (IFile f : files) mFileAdapter.add(new IFileDataModel(f));
mFileAdapter.notifyDataSetChanged();
// update footers
mFooterView.setVisibility(hasMoreFiles || mFileAdapter.isEmpty() ? View.VISIBLE : View.GONE);
if (hasMoreFiles)
mFooterView.setText(getString(R.string.afc_pmsg_max_file_count_allowed, mFileProvider.getMaxFileCount()));
else if (mFileAdapter.isEmpty())
mFooterView.setText(R.string.afc_msg_empty);
/*
* We use a Runnable to make sure this work. Because if the list
* view is handling data, this might not work.
*/
mViewFiles.post(new Runnable() {
@Override
public void run() {
if (shouldBeSelectedIdx >= 0 && shouldBeSelectedIdx < mFileAdapter.getCount()) {
mViewFiles.setSelection(shouldBeSelectedIdx);
} else if (!mFileAdapter.isEmpty())
mViewFiles.setSelection(0);
}
});
/*
* navigation buttons
*/
createLocationButtons(path);
/*
* update UI elements
*/
updateUI(path);
if (listener != null)
listener.onFinish(true, path);
}
}.execute();
}
use of group.pals.android.lib.ui.filechooser.io.IFileFilter in project FBReaderJ by geometer.
the class LocalFileProvider method listAllFiles.
// listAllFiles()
@Override
public List<IFile> listAllFiles(IFile dir, final IFileFilter filter) {
if (!(dir instanceof File))
return null;
final List<IFile> _res = new ArrayList<IFile>();
try {
IFile root = dir.parentFile();
if (root == null || filter == null || filter.accept(root))
_res.add(root);
File[] files = ((File) dir).listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
LocalFile file = new LocalFile(pathname);
if (filter == null || filter.accept(file))
_res.add(file);
return false;
}
});
if (files != null)
return _res;
return null;
} catch (Throwable t) {
return null;
}
}
Aggregations