Search in sources :

Example 1 with FileComparator

use of group.pals.android.lib.ui.filechooser.utils.FileComparator 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();
}
Also used : IFileFilter(group.pals.android.lib.ui.filechooser.io.IFileFilter) IFile(group.pals.android.lib.ui.filechooser.io.IFile) ArrayList(java.util.ArrayList) FileComparator(group.pals.android.lib.ui.filechooser.utils.FileComparator) LoadingDialog(group.pals.android.lib.ui.filechooser.utils.ui.LoadingDialog)

Example 2 with FileComparator

use of group.pals.android.lib.ui.filechooser.utils.FileComparator in project FBReaderJ by geometer.

the class LocalFileProvider method listAllFiles.

// listFiles()
@Override
public List<IFile> listAllFiles(IFile dir, final boolean[] hasMoreFiles) throws Exception {
    if (!(dir instanceof File) || !dir.canRead())
        return null;
    if (hasMoreFiles != null && hasMoreFiles.length > 0)
        hasMoreFiles[0] = false;
    final List<IFile> _files = new ArrayList<IFile>();
    try {
        IFile root = dir.parentFile();
        if (root.parentFile() == null && LocalFileProvider.this.accept(root))
            _files.add(root);
        File[] files = ((File) dir).listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                LocalFile file = new LocalFile(pathname);
                if (!LocalFileProvider.this.accept(file))
                    return false;
                if (_files.size() >= getMaxFileCount()) {
                    if (hasMoreFiles != null && hasMoreFiles.length > 0)
                        hasMoreFiles[0] = true;
                    return false;
                }
                _files.add(file);
                return false;
            }
        });
        if (files != null) {
            Collections.sort(_files, new FileComparator(getSortType(), getSortOrder()));
            return _files;
        }
        return null;
    } catch (Throwable t) {
        return null;
    }
}
Also used : LocalFile(group.pals.android.lib.ui.filechooser.io.localfile.LocalFile) IFile(group.pals.android.lib.ui.filechooser.io.IFile) ArrayList(java.util.ArrayList) FileComparator(group.pals.android.lib.ui.filechooser.utils.FileComparator) FileFilter(java.io.FileFilter) IFileFilter(group.pals.android.lib.ui.filechooser.io.IFileFilter) LocalFile(group.pals.android.lib.ui.filechooser.io.localfile.LocalFile) IFile(group.pals.android.lib.ui.filechooser.io.IFile) File(java.io.File) ParentFile(group.pals.android.lib.ui.filechooser.io.localfile.ParentFile)

Aggregations

IFile (group.pals.android.lib.ui.filechooser.io.IFile)2 IFileFilter (group.pals.android.lib.ui.filechooser.io.IFileFilter)2 FileComparator (group.pals.android.lib.ui.filechooser.utils.FileComparator)2 ArrayList (java.util.ArrayList)2 LocalFile (group.pals.android.lib.ui.filechooser.io.localfile.LocalFile)1 ParentFile (group.pals.android.lib.ui.filechooser.io.localfile.ParentFile)1 LoadingDialog (group.pals.android.lib.ui.filechooser.utils.ui.LoadingDialog)1 File (java.io.File)1 FileFilter (java.io.FileFilter)1