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