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
try {
documentProvider = provider[0]; documentProvider = provider[0];
homeDirectory = documentProvider.getRootDirectory(); homeDirectory = documentProvider.getRootDirectory();
currentDirectory = homeDirectory; currentDirectory = homeDirectory;
filePaths = currentDirectory.listFiles(FileUtilities filePaths = currentDirectory.listFiles(FileUtilities
.getFileFilter(filterMode)); .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];
try {
filePaths = currentDirectory.listFiles(FileUtilities filePaths = currentDirectory.listFiles(FileUtilities
.getFileFilter(filterMode)); .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,11 +303,26 @@ public class LibreOfficeUIActivity extends LOAbout implements ActionBar.OnNaviga ...@@ -276,11 +303,26 @@ 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
try {
return document[0].getDocument(); 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) {
if (file != null) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file)); Intent i = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
String packageName = getApplicationContext().getPackageName(); String packageName = getApplicationContext().getPackageName();
ComponentName componentName = new ComponentName(packageName, ComponentName componentName = new ComponentName(packageName,
...@@ -288,6 +330,7 @@ public class LibreOfficeUIActivity extends LOAbout implements ActionBar.OnNaviga ...@@ -288,6 +330,7 @@ public class LibreOfficeUIActivity extends LOAbout implements ActionBar.OnNaviga
i.setComponent(componentName); i.setComponent(componentName);
startActivity(i); 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