Search in sources :

Example 1 with TaskListener

use of group.pals.android.lib.ui.filechooser.utils.ui.TaskListener 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)

Aggregations

ComponentName (android.content.ComponentName)1 Intent (android.content.Intent)1 ServiceConnection (android.content.ServiceConnection)1 IBinder (android.os.IBinder)1 IFile (group.pals.android.lib.ui.filechooser.io.IFile)1 LoadingDialog (group.pals.android.lib.ui.filechooser.utils.ui.LoadingDialog)1 TaskListener (group.pals.android.lib.ui.filechooser.utils.ui.TaskListener)1