summaryrefslogtreecommitdiff
path: root/android
diff options
context:
space:
mode:
authorJacobo Aragunde Pérez <jaragunde@igalia.com>2015-02-09 20:20:11 +0100
committerJacobo Aragunde Pérez <jaragunde@igalia.com>2015-06-10 17:17:25 +0000
commitf8a4e1aab608538567562d97e6da60f015337c47 (patch)
treecf91ce35c74760cce2e0d36d4f751b57ae50e422 /android
parentba44368da5933040080da611aa1722491f89df69 (diff)
Android: document providers listen to changes in preferences.
With this patch, document providers are able to listen to changes in their preferences and update their internal state accordingly. Now ownCloud provider can see its server updated without restarting the application. Change-Id: I833c7ec9fc97be58bdc8ac2cbf4384a33c2b400e Reviewed-on: https://gerrit.libreoffice.org/16196 Reviewed-by: Jacobo Aragunde Pérez <jaragunde@igalia.com> Tested-by: Jacobo Aragunde Pérez <jaragunde@igalia.com>
Diffstat (limited to 'android')
-rw-r--r--android/source/src/java/org/libreoffice/storage/DocumentProviderFactory.java14
-rw-r--r--android/source/src/java/org/libreoffice/storage/DocumentProviderSettingsActivity.java27
-rw-r--r--android/source/src/java/org/libreoffice/storage/owncloud/OwnCloudProvider.java42
3 files changed, 77 insertions, 6 deletions
diff --git a/android/source/src/java/org/libreoffice/storage/DocumentProviderFactory.java b/android/source/src/java/org/libreoffice/storage/DocumentProviderFactory.java
index 3d462e655f86..612eaa6260f5 100644
--- a/android/source/src/java/org/libreoffice/storage/DocumentProviderFactory.java
+++ b/android/source/src/java/org/libreoffice/storage/DocumentProviderFactory.java
@@ -9,11 +9,15 @@
package org.libreoffice.storage;
+import java.util.HashSet;
+import java.util.Set;
+
import org.libreoffice.storage.local.LocalDocumentsDirectoryProvider;
import org.libreoffice.storage.local.LocalDocumentsProvider;
import org.libreoffice.storage.owncloud.OwnCloudProvider;
import android.content.Context;
+import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
/**
* Keeps the instances of the available IDocumentProviders in the system.
@@ -103,4 +107,14 @@ public final class DocumentProviderFactory {
public IDocumentProvider getDefaultProvider() {
return providers[0];
}
+
+ public Set<OnSharedPreferenceChangeListener> getChangeListeners() {
+ Set<OnSharedPreferenceChangeListener> listeners =
+ new HashSet<OnSharedPreferenceChangeListener>();
+ for (IDocumentProvider provider : providers) {
+ if (provider instanceof OnSharedPreferenceChangeListener)
+ listeners.add((OnSharedPreferenceChangeListener) provider);
+ }
+ return listeners;
+ }
}
diff --git a/android/source/src/java/org/libreoffice/storage/DocumentProviderSettingsActivity.java b/android/source/src/java/org/libreoffice/storage/DocumentProviderSettingsActivity.java
index d17fe510e857..e98534a44756 100644
--- a/android/source/src/java/org/libreoffice/storage/DocumentProviderSettingsActivity.java
+++ b/android/source/src/java/org/libreoffice/storage/DocumentProviderSettingsActivity.java
@@ -9,11 +9,15 @@
package org.libreoffice.storage;
+import java.util.Set;
+
import org.libreoffice.R;
import android.app.Activity;
+import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceFragment;
+import android.preference.PreferenceManager;
public class DocumentProviderSettingsActivity extends Activity {
@@ -21,6 +25,8 @@ public class DocumentProviderSettingsActivity extends Activity {
public static final String KEY_PREF_OWNCLOUD_USER_NAME = "pref_user_name";
public static final String KEY_PREF_OWNCLOUD_PASSWORD = "pref_password";
+ private Set<OnSharedPreferenceChangeListener> listeners;
+
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -30,6 +36,27 @@ public class DocumentProviderSettingsActivity extends Activity {
.replace(android.R.id.content, new SettingsFragment()).commit();
}
+ @Override
+ protected void onResume() {
+ super.onResume();
+
+ listeners = DocumentProviderFactory.getInstance().getChangeListeners();
+ for (OnSharedPreferenceChangeListener listener : listeners) {
+ PreferenceManager.getDefaultSharedPreferences(this)
+ .registerOnSharedPreferenceChangeListener(listener);
+ }
+ }
+
+ @Override
+ protected void onPause() {
+ super.onPause();
+
+ for (OnSharedPreferenceChangeListener listener : listeners) {
+ PreferenceManager.getDefaultSharedPreferences(this)
+ .unregisterOnSharedPreferenceChangeListener(listener);
+ }
+ }
+
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
diff --git a/android/source/src/java/org/libreoffice/storage/owncloud/OwnCloudProvider.java b/android/source/src/java/org/libreoffice/storage/owncloud/OwnCloudProvider.java
index a94e1add9a0e..827c0aff22d4 100644
--- a/android/source/src/java/org/libreoffice/storage/owncloud/OwnCloudProvider.java
+++ b/android/source/src/java/org/libreoffice/storage/owncloud/OwnCloudProvider.java
@@ -10,6 +10,7 @@ import org.libreoffice.storage.IFile;
import android.content.Context;
import android.content.SharedPreferences;
+import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.net.Uri;
import android.preference.PreferenceManager;
@@ -24,8 +25,10 @@ import com.owncloud.android.lib.resources.files.RemoteFile;
/**
* Implementation of IDocumentProvider for ownCloud servers.
*/
-public class OwnCloudProvider implements IDocumentProvider {
+public class OwnCloudProvider implements IDocumentProvider,
+ OnSharedPreferenceChangeListener {
+ private Context context;
private OwnCloudClient client;
private File cacheDir;
@@ -34,6 +37,8 @@ public class OwnCloudProvider implements IDocumentProvider {
private String password;
public OwnCloudProvider(Context context) {
+ this.context = context;
+
// read preferences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
serverUrl = preferences.getString(
@@ -43,11 +48,7 @@ public class OwnCloudProvider implements IDocumentProvider {
password = preferences.getString(
DocumentProviderSettingsActivity.KEY_PREF_OWNCLOUD_PASSWORD, "");
- Uri serverUri = Uri.parse(serverUrl);
- client = OwnCloudClientFactory.createOwnCloudClient(serverUri,
- context, true);
- client.setCredentials(OwnCloudCredentialsFactory.newBasicCredentials(
- userName, password));
+ setupClient();
// make sure cache directory exists, and clear it
// TODO: probably we should do smarter cache management
@@ -58,6 +59,14 @@ public class OwnCloudProvider implements IDocumentProvider {
cacheDir.mkdirs();
}
+ private void setupClient() {
+ Uri serverUri = Uri.parse(serverUrl);
+ client = OwnCloudClientFactory.createOwnCloudClient(serverUri, context,
+ true);
+ client.setCredentials(OwnCloudCredentialsFactory.newBasicCredentials(
+ userName, password));
+ }
+
@Override
public IFile getRootDirectory() {
return createFromUri(URI.create(FileUtils.PATH_SEPARATOR));
@@ -116,4 +125,25 @@ public class OwnCloudProvider implements IDocumentProvider {
}
file.delete();
}
+
+ @Override
+ public void onSharedPreferenceChanged(SharedPreferences preferences,
+ String key) {
+ boolean changed = false;
+ if (key.equals(DocumentProviderSettingsActivity.KEY_PREF_OWNCLOUD_SERVER)) {
+ serverUrl = preferences.getString(key, "");
+ changed = true;
+ }
+ else if (key.equals(DocumentProviderSettingsActivity.KEY_PREF_OWNCLOUD_USER_NAME)) {
+ userName = preferences.getString(key, "");
+ changed = true;
+ }
+ else if (key.equals(DocumentProviderSettingsActivity.KEY_PREF_OWNCLOUD_PASSWORD)) {
+ password = preferences.getString(key, "");
+ changed = true;
+ }
+
+ if (changed)
+ setupClient();
+ }
}