[Libreoffice-commits] core.git: Branch 'feature/owncloud-provider-for-android' - android/experimental
Jacobo Aragunde Pérez
jaragunde at igalia.com
Tue Feb 10 08:20:00 PST 2015
android/experimental/LOAndroid3/res/values/strings.xml | 4
android/experimental/LOAndroid3/src/java/org/libreoffice/storage/IDocumentProvider.java | 2
android/experimental/LOAndroid3/src/java/org/libreoffice/storage/IFile.java | 3
android/experimental/LOAndroid3/src/java/org/libreoffice/storage/owncloud/OwnCloudFile.java | 8 -
android/experimental/LOAndroid3/src/java/org/libreoffice/storage/owncloud/OwnCloudProvider.java | 27 +++
android/experimental/LOAndroid3/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java | 71 ++++++++--
6 files changed, 96 insertions(+), 19 deletions(-)
New commits:
commit 706d2d9873b2ec3c48b49e2d0781b9a21bf58e8d
Author: Jacobo Aragunde Pérez <jaragunde at igalia.com>
Date: Tue Feb 10 16:17:12 2015 +0000
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.
This patch implements error management for ownCloud provider.
Change-Id: Iad7249dbdc06b2a0890d5435ad65284b728e9707
diff --git a/android/experimental/LOAndroid3/res/values/strings.xml b/android/experimental/LOAndroid3/res/values/strings.xml
index 336b19b..135e52d 100644
--- a/android/experimental/LOAndroid3/res/values/strings.xml
+++ b/android/experimental/LOAndroid3/res/values/strings.xml
@@ -36,6 +36,10 @@
<string name="local_file_system">Local file system</string>
<string name="owncloud">ownCloud</string>
+ <string name="owncloud_wrong_connection">Cannot connect to ownCloud server. Check your configuration.</string>
+ <string name="owncloud_unauthorized">Cannot log into ownCloud server. Check your configuration.</string>
+ <string name="owncloud_unspecified_error">Unspecified error connecting to ownCloud server. Check your configuration and/or try later.</string>
+
<!-- Document provider settings -->
<string name="storage_provider_settings">Storage provider settings</string>
<string name="owncloud_settings">ownCloud settings</string>
diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/IDocumentProvider.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/IDocumentProvider.java
index 191a143..bbfdecd 100644
--- a/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/IDocumentProvider.java
+++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/IDocumentProvider.java
@@ -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);
diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/IFile.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/IFile.java
index 5b71c09..8effd0f 100644
--- a/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/IFile.java
+++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/IFile.java
@@ -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();
}
diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/owncloud/OwnCloudFile.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/owncloud/OwnCloudFile.java
index a8d1a06..ce10ab6 100644
--- a/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/owncloud/OwnCloudFile.java
+++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/owncloud/OwnCloudFile.java
@@ -69,8 +69,7 @@ public class OwnCloudFile implements IFile {
RemoteOperationResult result = refreshOperation.execute(provider
.getClient());
if (!result.isSuccess()) {
- throw new RuntimeException(result.getLogMessage(),
- result.getException());
+ throw provider.buildRuntimeExceptionForResultCode(result.getCode());
}
for (Object obj : result.getData()) {
RemoteFile child = (RemoteFile) obj;
@@ -104,7 +103,10 @@ public class OwnCloudFile implements IFile {
File downFolder = provider.getCacheDir();
DownloadRemoteFileOperation operation = new DownloadRemoteFileOperation(
file.getRemotePath(), downFolder.getAbsolutePath());
- operation.execute(provider.getClient());
+ RemoteOperationResult result = operation.execute(provider.getClient());
+ if (!result.isSuccess()) {
+ throw provider.buildRuntimeExceptionForResultCode(result.getCode());
+ }
return new File(downFolder.getAbsolutePath() + file.getRemotePath());
}
diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/owncloud/OwnCloudProvider.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/owncloud/OwnCloudProvider.java
index 827c0af..66e4633 100644
--- a/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/owncloud/OwnCloudProvider.java
+++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/storage/owncloud/OwnCloudProvider.java
@@ -18,6 +18,7 @@ import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
+import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
import com.owncloud.android.lib.resources.files.FileUtils;
import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation;
import com.owncloud.android.lib.resources.files.RemoteFile;
@@ -78,8 +79,7 @@ public class OwnCloudProvider implements IDocumentProvider,
uri.getPath());
RemoteOperationResult result = refreshOperation.execute(client);
if (!result.isSuccess()) {
- throw new RuntimeException(result.getLogMessage(),
- result.getException());
+ throw buildRuntimeExceptionForResultCode(result.getCode());
}
if (result.getData().size() > 0) {
return new OwnCloudFile(this, (RemoteFile) result.getData().get(0));
@@ -113,6 +113,29 @@ public class OwnCloudProvider implements IDocumentProvider,
}
/**
+ * Build the proper RuntimeException for some error result.
+ *
+ * @param code Result code got from some RemoteOperationResult.
+ * @return exception with the proper internationalized error message.
+ */
+ protected RuntimeException buildRuntimeExceptionForResultCode(ResultCode code) {
+ int errorMessage;
+ switch (code) {
+ case WRONG_CONNECTION: // SocketException
+ case FILE_NOT_FOUND: // HTTP 404
+ errorMessage = R.string.owncloud_wrong_connection;
+ break;
+ case UNAUTHORIZED: // wrong user/pass
+ errorMessage = R.string.owncloud_unauthorized;
+ break;
+ default:
+ errorMessage = R.string.owncloud_unspecified_error;
+ break;
+ }
+ return new RuntimeException(context.getString(errorMessage));
+ }
+
+ /**
* Deletes files and recursively deletes directories.
*
* @param file
diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java
index 360ce0b..ec2e27a 100644
--- a/android/experimental/LOAndroid3/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java
+++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/ui/LibreOfficeUIActivity.java
@@ -66,6 +66,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;
@@ -236,11 +237,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;
}
@@ -262,8 +276,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;
}
@@ -280,17 +307,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);
}
More information about the Libreoffice-commits
mailing list