use of group.pals.android.lib.ui.filechooser.utils.ui.LoadingDialog 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.utils.ui.LoadingDialog 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();
}
Aggregations