Search in sources :

Example 1 with IFile

use of group.pals.android.lib.ui.filechooser.io.IFile in project FBReaderJ by geometer.

the class FileChooserUtil method filePathsFromData.

public static List<String> filePathsFromData(Intent data) {
    final List<IFile> files = data.getParcelableArrayListExtra(FileChooserActivity._Results);
    final List<String> paths = new ArrayList<String>(files.size());
    for (IFile f : files) {
        paths.add(f.getAbsolutePath());
    }
    return paths;
}
Also used : IFile(group.pals.android.lib.ui.filechooser.io.IFile)

Example 2 with IFile

use of group.pals.android.lib.ui.filechooser.io.IFile in project FBReaderJ by geometer.

the class FileChooserActivity method doCreateNewDir.

// doSwitchViewType()
/**
     * Confirms user to create new directory.
     */
private void doCreateNewDir() {
    if (mFileProvider instanceof LocalFileProvider && !Utils.hasPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        Dlg.toast(this, R.string.afc_msg_app_doesnot_have_permission_to_create_files, Dlg._LengthShort);
        return;
    }
    if ((getLocation() instanceof File)) {
        if (!((File) getLocation()).canWrite()) {
            Dlg.toast(this, R.string.afc_msg_app_cant_create_folder, Dlg._LengthShort);
            return;
        }
    }
    final AlertDialog _dlg = Dlg.newDlg(this);
    View view = getLayoutInflater().inflate(R.layout.afc_simple_text_input_view, null);
    final EditText _textFile = (EditText) view.findViewById(R.id.afc_simple_text_input_view_text1);
    _textFile.setHint(mTextResources.get("folderNameHint"));
    _textFile.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                Ui.hideSoftKeyboard(FileChooserActivity.this, _textFile.getWindowToken());
                _dlg.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
                return true;
            }
            return false;
        }
    });
    _dlg.setView(view);
    _dlg.setTitle(mTextResources.get("newFolder"));
    _dlg.setIcon(android.R.drawable.ic_menu_add);
    _dlg.setButton(DialogInterface.BUTTON_POSITIVE, getString(android.R.string.ok), new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            String name = _textFile.getText().toString().trim();
            if (!FileUtils.isFilenameValid(name)) {
                Dlg.toast(FileChooserActivity.this, getString(R.string.afc_pmsg_filename_is_invalid, name), Dlg._LengthShort);
                return;
            }
            final IFileProvider fileProvider = mFileProvider;
            final IFile location = getLocation();
            if (fileProvider == null || location == null) {
                return;
            }
            IFile dir = fileProvider.fromPath(String.format("%s/%s", location.getAbsolutePath(), name));
            if (dir.mkdir()) {
                Dlg.toast(FileChooserActivity.this, getString(R.string.afc_msg_done), Dlg._LengthShort);
                setLocation(getLocation(), null);
            } else
                Dlg.toast(FileChooserActivity.this, getString(R.string.afc_pmsg_cannot_create_folder, name), Dlg._LengthShort);
        }
    });
    _dlg.show();
    final Button _btnOk = _dlg.getButton(DialogInterface.BUTTON_POSITIVE);
    _btnOk.setEnabled(false);
    _textFile.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
            _btnOk.setEnabled(FileUtils.isFilenameValid(s.toString().trim()));
        }
    });
}
Also used : AlertDialog(android.app.AlertDialog) EditText(android.widget.EditText) IFileProvider(group.pals.android.lib.ui.filechooser.services.IFileProvider) IFile(group.pals.android.lib.ui.filechooser.io.IFile) DialogInterface(android.content.DialogInterface) LocalFileProvider(group.pals.android.lib.ui.filechooser.services.LocalFileProvider) GridView(android.widget.GridView) ImageView(android.widget.ImageView) HorizontalScrollView(android.widget.HorizontalScrollView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) AbsListView(android.widget.AbsListView) KeyEvent(android.view.KeyEvent) Button(android.widget.Button) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) TextView(android.widget.TextView) IFile(group.pals.android.lib.ui.filechooser.io.IFile) File(java.io.File)

Example 3 with IFile

use of group.pals.android.lib.ui.filechooser.io.IFile in project FBReaderJ by geometer.

the class FileChooserActivity method setupService.

// bindService()
/**
     * Setup the file provider:<br>
     * - filter mode;<br>
     * - display hidden files;<br>
     * - max file count;<br>
     * - ...
     */
private void setupService() {
    /*
         * set root path, if not specified, try using
         * IFileProvider#defaultPath()
         */
    if (getIntent().getParcelableExtra(_Rootpath) != null)
        mRoot = (IFile) getIntent().getSerializableExtra(_Rootpath);
    if (mRoot == null || !mRoot.isDirectory())
        mRoot = mFileProvider.defaultPath();
    IFileProvider.FilterMode filterMode = (FilterMode) getIntent().getSerializableExtra(_FilterMode);
    if (filterMode == null) {
        filterMode = IFileProvider.FilterMode.DirectoriesOnly;
    }
    IFileProvider.SortType sortType = DisplayPrefs.getSortType(this);
    boolean sortAscending = DisplayPrefs.isSortAscending(this);
    mFileProvider.setDisplayHiddenFiles(getIntent().getBooleanExtra(_DisplayHiddenFiles, false));
    mFileProvider.setFilterMode(mIsSaveDialog ? IFileProvider.FilterMode.FilesOnly : filterMode);
    mFileProvider.setMaxFileCount(getIntent().getIntExtra(_MaxFileCount, 1024));
    mFileProvider.setRegexFilenameFilter(getIntent().getStringExtra(_RegexFilenameFilter));
    mFileProvider.setSortOrder(sortAscending ? IFileProvider.SortOrder.Ascending : IFileProvider.SortOrder.Descending);
    mFileProvider.setSortType(sortType);
}
Also used : IFileProvider(group.pals.android.lib.ui.filechooser.services.IFileProvider) IFile(group.pals.android.lib.ui.filechooser.io.IFile) SortType(group.pals.android.lib.ui.filechooser.services.IFileProvider.SortType) FilterMode(group.pals.android.lib.ui.filechooser.services.IFileProvider.FilterMode) FilterMode(group.pals.android.lib.ui.filechooser.services.IFileProvider.FilterMode)

Example 4 with IFile

use of group.pals.android.lib.ui.filechooser.io.IFile 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 5 with IFile

use of group.pals.android.lib.ui.filechooser.io.IFile 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)13 ArrayList (java.util.ArrayList)6 File (java.io.File)5 IFileFilter (group.pals.android.lib.ui.filechooser.io.IFileFilter)4 LocalFile (group.pals.android.lib.ui.filechooser.io.localfile.LocalFile)3 ParentFile (group.pals.android.lib.ui.filechooser.io.localfile.ParentFile)3 FileFilter (java.io.FileFilter)3 AlertDialog (android.app.AlertDialog)2 DialogInterface (android.content.DialogInterface)2 Intent (android.content.Intent)2 View (android.view.View)2 AdapterView (android.widget.AdapterView)2 ListView (android.widget.ListView)2 IFileProvider (group.pals.android.lib.ui.filechooser.services.IFileProvider)2 FileComparator (group.pals.android.lib.ui.filechooser.utils.FileComparator)2 LoadingDialog (group.pals.android.lib.ui.filechooser.utils.ui.LoadingDialog)2 ComponentName (android.content.ComponentName)1 ServiceConnection (android.content.ServiceConnection)1 IBinder (android.os.IBinder)1 Editable (android.text.Editable)1