[Libreoffice-commits] core.git: android/source

Jacobo Aragunde Pérez jaragunde at igalia.com
Wed Jun 10 10:17:54 PDT 2015


 android/source/src/java/org/libreoffice/storage/DocumentProviderFactory.java          |   14 +++
 android/source/src/java/org/libreoffice/storage/DocumentProviderSettingsActivity.java |   27 ++++++
 android/source/src/java/org/libreoffice/storage/owncloud/OwnCloudProvider.java        |   42 ++++++++--
 3 files changed, 77 insertions(+), 6 deletions(-)

New commits:
commit f8a4e1aab608538567562d97e6da60f015337c47
Author: Jacobo Aragunde Pérez <jaragunde at igalia.com>
Date:   Mon Feb 9 20:20:11 2015 +0100

    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 at igalia.com>
    Tested-by: Jacobo Aragunde Pérez <jaragunde at igalia.com>

diff --git a/android/source/src/java/org/libreoffice/storage/DocumentProviderFactory.java b/android/source/src/java/org/libreoffice/storage/DocumentProviderFactory.java
index 3d462e6..612eaa6 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 d17fe51..e98534a 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 a94e1ad..827c0af 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();
+    }
 }


More information about the Libreoffice-commits mailing list