Search in sources :

Example 6 with IFile

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

the class ViewFilesContextMenuUtils method doShowHistoryContents.

/**
     * Shows history contents to the user. He can clear all items.
     * 
     * @param context
     *            {@link Context}
     * @param fileProvider
     *            {@link IFileProvider}
     * @param history
     *            {@link History} of {@link IFile}.
     * @param currentLocation
     *            current location, will not be shown.
     * @param listener
     *            will be notified after the user closed the dialog, or when the
     *            user selects an item.
     */
public static void doShowHistoryContents(final Context context, final IFileProvider fileProvider, final History<IFile> history, IFile currentLocation, final TaskListener listener) {
    if (history.isEmpty())
        return;
    final AlertDialog _dialog = Dlg.newDlg(context);
    // don't use Cancel button
    _dialog.setButton(DialogInterface.BUTTON_NEGATIVE, null, (DialogInterface.OnClickListener) null);
    _dialog.setIcon(android.R.drawable.ic_dialog_info);
    _dialog.setTitle(R.string.afc_title_history);
    List<IFileDataModel> data = new ArrayList<IFileDataModel>();
    ArrayList<IFile> items = history.items();
    for (int i = items.size() - 1; i >= 0; i--) {
        IFile f = items.get(i);
        if (f == currentLocation)
            continue;
        // check for duplicates
        boolean duplicated = false;
        for (int j = 0; j < data.size(); j++) {
            if (f.equalsToPath(data.get(j).getFile())) {
                duplicated = true;
                break;
            }
        }
        if (!duplicated)
            data.add(new IFileDataModel(f));
    }
    final IFileAdapter _adapter = new IFileAdapter(context, data, FilterMode.DirectoriesOnly, null, false);
    ListView listView = (ListView) LayoutInflater.from(context).inflate(R.layout.afc_listview_files, null);
    listView.setBackgroundResource(0);
    listView.setFastScrollEnabled(true);
    listView.setAdapter(_adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (listener != null) {
                _dialog.dismiss();
                listener.onFinish(true, _adapter.getItem(position).getFile());
            }
        }
    });
    // OnItemClickListener
    _dialog.setView(listView);
    _dialog.setButton(DialogInterface.BUTTON_POSITIVE, context.getString(R.string.afc_cmd_clear), new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            history.clear();
        }
    });
    _dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {
            if (listener != null)
                listener.onFinish(true, null);
        }
    });
    _dialog.show();
}
Also used : AlertDialog(android.app.AlertDialog) IFileAdapter(group.pals.android.lib.ui.filechooser.IFileAdapter) IFile(group.pals.android.lib.ui.filechooser.io.IFile) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) ListView(android.widget.ListView) IFileDataModel(group.pals.android.lib.ui.filechooser.IFileDataModel) AdapterView(android.widget.AdapterView)

Example 7 with IFile

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

the class FileChooserActivity method doFinish.

// refreshHistories()
/**
     * Finishes this activity.
     * 
     * @param files
     *            list of {@link IFile}
     */
private void doFinish(IFile... files) {
    final List<IFile> list = new ArrayList<IFile>();
    for (IFile f : files) list.add(f);
    doFinish((ArrayList<IFile>) list);
}
Also used : IFile(group.pals.android.lib.ui.filechooser.io.IFile) ArrayList(java.util.ArrayList)

Example 8 with IFile

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

the class FileChooserActivity method doFinish.

/**
     * Finishes this activity.
     * 
     * @param files
     *            list of {@link IFile}
     */
private void doFinish(ArrayList<IFile> files) {
    String returnPath = null;
    // set results
    switch(mFileProvider.getFilterMode()) {
        case FilesOnly:
            if (files == null || files.isEmpty()) {
                setResult(RESULT_CANCELED);
                finish();
                return;
            }
            break;
        case DirectoriesOnly:
            {
                final File file = (File) getLocation();
                if (file != null && file.canWrite()) {
                    returnPath = getLocation().getAbsolutePath();
                }
                break;
            }
        case FilesAndDirectories:
            if (files == null || files.isEmpty()) {
                returnPath = getLocation().getAbsolutePath();
            }
            break;
        default:
            returnPath = getLocation().getAbsolutePath();
            break;
    }
    boolean hasData = false;
    Intent intent = new Intent();
    if (returnPath != null) {
        intent.putExtra(_FolderPath, returnPath);
        hasData = true;
    }
    if (files != null) {
        intent.putExtra(_Results, files);
        hasData = true;
    } else {
        intent.putExtra(_Results, new ArrayList<IFile>());
    }
    if (!hasData) {
        return;
    }
    // return flags for further use (in case the caller needs)
    intent.putExtra(_FilterMode, mFileProvider.getFilterMode());
    intent.putExtra(_SaveDialog, mIsSaveDialog);
    setResult(RESULT_OK, intent);
    if (DisplayPrefs.isRememberLastLocation(this) && getLocation() != null) {
        DisplayPrefs.setLastLocation(this, getLocation().getAbsolutePath());
    } else
        DisplayPrefs.setLastLocation(this, null);
    finish();
}
Also used : IFile(group.pals.android.lib.ui.filechooser.io.IFile) Intent(android.content.Intent) IFile(group.pals.android.lib.ui.filechooser.io.IFile) File(java.io.File)

Example 9 with IFile

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

the class FileChooserActivity method bindService.

/**
     * Connects to file provider service, then loads root directory. If can not,
     * then finishes this activity with result code =
     * {@link Activity#RESULT_CANCELED}
     * 
     * @param savedInstanceState
     */
private void bindService(final Bundle savedInstanceState) {
    if (startService(new Intent(this, mFileProviderServiceClass)) == null) {
        doShowCannotConnectToServiceAndFinish();
        return;
    }
    mServiceConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className, IBinder service) {
            try {
                mFileProvider = ((FileProviderService.LocalBinder) service).getService();
            } catch (Throwable t) {
                Log.e(_ClassName, "mServiceConnection.onServiceConnected() -> " + t);
            }
        }

        // onServiceConnected()
        public void onServiceDisconnected(ComponentName className) {
            mFileProvider = null;
        }
    };
    bindService(new Intent(this, mFileProviderServiceClass), mServiceConnection, Context.BIND_AUTO_CREATE);
    new LoadingDialog(this, R.string.afc_msg_loading, false) {

        private static final int _WaitTime = 200;

        // 3 seconds
        private static final int _MaxWaitTime = 3000;

        @Override
        protected Object doInBackground(Void... params) {
            int totalWaitTime = 0;
            while (mFileProvider == null) {
                try {
                    totalWaitTime += _WaitTime;
                    Thread.sleep(_WaitTime);
                    if (totalWaitTime >= _MaxWaitTime)
                        break;
                } catch (InterruptedException e) {
                    break;
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object result) {
            super.onPostExecute(result);
            if (mFileProvider == null) {
                doShowCannotConnectToServiceAndFinish();
            } else {
                setupService();
                setupHeader();
                setupViewFiles();
                setupFooter();
                /*
                     * Priorities for starting path:
                     * 
                     * 1. Current location (in case the activity has been killed
                     * after configurations changed).
                     * 
                     * 2. Selected file from key _SelectFile.
                     * 
                     * 3. Last location.
                     * 
                     * 4. Root path from key _Rootpath.
                     */
                // current location
                IFile path = savedInstanceState != null ? (IFile) savedInstanceState.get(_CurrentLocation) : null;
                // selected file
                IFile selectedFile = null;
                if (path == null) {
                    selectedFile = (IFile) getIntent().getParcelableExtra(_SelectFile);
                    if (selectedFile != null && selectedFile.exists())
                        path = selectedFile.parentFile();
                    if (path == null)
                        selectedFile = null;
                }
                // last location
                if (path == null && DisplayPrefs.isRememberLastLocation(FileChooserActivity.this)) {
                    String lastLocation = DisplayPrefs.getLastLocation(FileChooserActivity.this);
                    if (lastLocation != null)
                        path = mFileProvider.fromPath(lastLocation);
                }
                final IFile _selectedFile = selectedFile;
                // or root path
                setLocation(path != null && path.isDirectory() ? path : mRoot, new TaskListener() {

                    @Override
                    public void onFinish(boolean ok, Object any) {
                        if (ok && _selectedFile != null && _selectedFile.isFile() && mIsSaveDialog)
                            mTxtSaveas.setText(_selectedFile.getName());
                        // don't push current location into history
                        boolean isCurrentLocation = savedInstanceState != null && any.equals(savedInstanceState.get(_CurrentLocation));
                        if (isCurrentLocation) {
                            mHistory.notifyHistoryChanged();
                        } else {
                            mHistory.push((IFile) any);
                            mFullHistory.push((IFile) any);
                        }
                    }
                }, selectedFile);
            }
        }
    }.execute();
}
Also used : ServiceConnection(android.content.ServiceConnection) IFile(group.pals.android.lib.ui.filechooser.io.IFile) Intent(android.content.Intent) IBinder(android.os.IBinder) TaskListener(group.pals.android.lib.ui.filechooser.utils.ui.TaskListener) ComponentName(android.content.ComponentName) LoadingDialog(group.pals.android.lib.ui.filechooser.utils.ui.LoadingDialog)

Example 10 with IFile

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

the class FileChooserActivity method onBackPressed.

// onStart()
@Override
public void onBackPressed() {
    IFile currentLoc = getLocation();
    if (currentLoc == null || mHistory == null) {
        super.onBackPressed();
        return;
    }
    IFile preLoc = null;
    while (currentLoc.equalsToPath(preLoc = mHistory.prevOf(currentLoc))) mHistory.remove(preLoc);
    if (preLoc != null) {
        goTo(preLoc);
    } else {
        super.onBackPressed();
    }
}
Also used : IFile(group.pals.android.lib.ui.filechooser.io.IFile)

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