Kaydet (Commit) 9e8fa856 authored tarafından Jacobo Aragunde Pérez's avatar Jacobo Aragunde Pérez

Android: improve error handling for document providers.

Now some operations in document providers may throw a RuntimeException
in case of error. The main activity is ready to catch them and show an
error message.

Change-Id: Iad7249dbdc06b2a0890d5435ad65284b728e9707
üst 8c5e8785
......@@ -21,6 +21,7 @@ public interface IDocumentProvider {
* Provides the content root element for the Document Provider.
*
* @return Content root element.
* @throws RuntimeException in case of error.
*/
IFile getRootDirectory();
......@@ -31,6 +32,7 @@ public interface IDocumentProvider {
* URI pointing to some content object that has been previously
* retrieved with IFile.getUri().
* @return IFile object pointing to the content represented by uri.
* @throws RuntimeException in case of error.
*/
IFile createFromUri(URI uri);
......
......@@ -71,6 +71,7 @@ public interface IFile {
*
* @return list of files contained by this directory, or an empty list if
* this is not a directory.
* @throws RuntimeException in case of error.
*/
List<IFile> listFiles();
......@@ -82,6 +83,7 @@ public interface IFile {
* the filter to match names against.
* @return filtered list of files contained by this directory, or an empty
* list if this is not a directory.
* @throws RuntimeException in case of error.
*/
List<IFile> listFiles(FileFilter filter);
......@@ -97,6 +99,7 @@ public interface IFile {
* for a directory is not defined.
*
* @return local file containing the document wrapped by this object.
* @throws RuntimeException in case of error.
*/
File getDocument();
}
......@@ -64,6 +64,7 @@ import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.net.URI;
import java.net.URISyntaxException;
......@@ -232,11 +233,24 @@ public class LibreOfficeUIActivity extends LOAbout implements ActionBar.OnNaviga
// switch document provider:
// these operations may imply network access and must be run in
// a different thread
documentProvider = provider[0];
homeDirectory = documentProvider.getRootDirectory();
currentDirectory = homeDirectory;
filePaths = currentDirectory.listFiles(FileUtilities
.getFileFilter(filterMode));
try {
documentProvider = provider[0];
homeDirectory = documentProvider.getRootDirectory();
currentDirectory = homeDirectory;
filePaths = currentDirectory.listFiles(FileUtilities
.getFileFilter(filterMode));
}
catch (final RuntimeException e) {
final Activity activity = LibreOfficeUIActivity.this;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, e.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
Log.e(tag, e.getMessage(), e.getCause());
}
return null;
}
......@@ -258,8 +272,21 @@ public class LibreOfficeUIActivity extends LOAbout implements ActionBar.OnNaviga
// this operation may imply network access and must be run in
// a different thread
currentDirectory = dir[0];
filePaths = currentDirectory.listFiles(FileUtilities
.getFileFilter(filterMode));
try {
filePaths = currentDirectory.listFiles(FileUtilities
.getFileFilter(filterMode));
}
catch (final RuntimeException e) {
final Activity activity = LibreOfficeUIActivity.this;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, e.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
Log.e(tag, e.getMessage(), e.getCause());
}
return null;
}
......@@ -276,17 +303,33 @@ public class LibreOfficeUIActivity extends LOAbout implements ActionBar.OnNaviga
protected File doInBackground(IFile... document) {
// this operation may imply network access and must be run in
// a different thread
return document[0].getDocument();
try {
return document[0].getDocument();
}
catch (final RuntimeException e) {
final Activity activity = LibreOfficeUIActivity.this;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, e.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
Log.e(tag, e.getMessage(), e.getCause());
return null;
}
}
@Override
protected void onPostExecute(File file) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
String packageName = getApplicationContext().getPackageName();
ComponentName componentName = new ComponentName(packageName,
LibreOfficeMainActivity.class.getName());
i.setComponent(componentName);
startActivity(i);
if (file != null) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
String packageName = getApplicationContext().getPackageName();
ComponentName componentName = new ComponentName(packageName,
LibreOfficeMainActivity.class.getName());
i.setComponent(componentName);
startActivity(i);
}
}
}.execute(document);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment