Modify OwncloudFileObserver for enforcing watching the file when an external app...
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileObserverService.java
index fa1fec2..a90e551 100644 (file)
@@ -22,15 +22,6 @@ import java.io.File;
 import java.util.HashMap;
 import java.util.Map;
 
 import java.util.HashMap;
 import java.util.Map;
 
-import com.owncloud.android.datamodel.FileDataStorageManager;
-import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
-import com.owncloud.android.files.OwnCloudFileObserver;
-import com.owncloud.android.operations.SynchronizeFileOperation;
-import com.owncloud.android.utils.FileStorageUtils;
-import com.owncloud.android.utils.Log_OC;
-
-
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.app.Service;
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.app.Service;
@@ -40,8 +31,18 @@ import android.content.Intent;
 import android.content.IntentFilter;
 import android.database.Cursor;
 import android.os.Binder;
 import android.content.IntentFilter;
 import android.database.Cursor;
 import android.os.Binder;
+import android.os.Handler;
 import android.os.IBinder;
 
 import android.os.IBinder;
 
+import com.owncloud.android.MainApp;
+import com.owncloud.android.datamodel.FileDataStorageManager;
+import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
+import com.owncloud.android.files.OwnCloudFileObserver;
+import com.owncloud.android.operations.SynchronizeFileOperation;
+import com.owncloud.android.utils.FileStorageUtils;
+import com.owncloud.android.utils.Log_OC;
+
 public class FileObserverService extends Service {
 
     public final static int CMD_INIT_OBSERVED_LIST = 1;
 public class FileObserverService extends Service {
 
     public final static int CMD_INIT_OBSERVED_LIST = 1;
@@ -57,6 +58,7 @@ public class FileObserverService extends Service {
     private static Map<String, OwnCloudFileObserver> mObserversMap;
     private static DownloadCompletedReceiverBis mDownloadReceiver;
     private IBinder mBinder = new LocalBinder();
     private static Map<String, OwnCloudFileObserver> mObserversMap;
     private static DownloadCompletedReceiverBis mDownloadReceiver;
     private IBinder mBinder = new LocalBinder();
+    private Handler mHandler = new Handler();
     
     public class LocalBinder extends Binder {
         FileObserverService getService() {
     
     public class LocalBinder extends Binder {
         FileObserverService getService() {
@@ -81,10 +83,10 @@ public class FileObserverService extends Service {
     
     @Override
     public void onDestroy() {
     
     @Override
     public void onDestroy() {
-        super.onDestroy();
         unregisterReceiver(mDownloadReceiver);
         mObserversMap = null;   // TODO study carefully the life cycle of Services to grant the best possible observance
         Log_OC.d(TAG, "Bye, bye");
         unregisterReceiver(mDownloadReceiver);
         mObserversMap = null;   // TODO study carefully the life cycle of Services to grant the best possible observance
         Log_OC.d(TAG, "Bye, bye");
+        super.onDestroy();
     }
     
     
     }
     
     
@@ -141,7 +143,7 @@ public class FileObserverService extends Service {
                 null);
         if (c == null || !c.moveToFirst()) return;
         AccountManager acm = AccountManager.get(this);
                 null);
         if (c == null || !c.moveToFirst()) return;
         AccountManager acm = AccountManager.get(this);
-        Account[] accounts = acm.getAccounts();
+        Account[] accounts = acm.getAccountsByType(MainApp.getAccountType());
         do {
             Account account = null;
             for (Account a : accounts)
         do {
             Account account = null;
             for (Account a : accounts)
@@ -159,10 +161,7 @@ public class FileObserverService extends Service {
             String path = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
             if (path == null || path.length() <= 0)
                 continue;
             String path = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
             if (path == null || path.length() <= 0)
                 continue;
-            OwnCloudFileObserver observer =
-                    new OwnCloudFileObserver(   path, 
-                                                account, 
-                                                getApplicationContext());
+            OwnCloudFileObserver observer = new OwnCloudFileObserver(path, account, getApplicationContext(), mHandler);
             mObserversMap.put(path, observer);
             if (new File(path).exists()) {
                 observer.startWatching();
             mObserversMap.put(path, observer);
             if (new File(path).exists()) {
                 observer.startWatching();
@@ -201,7 +200,8 @@ public class FileObserverService extends Service {
             /// the local file was never registered to observe before
             observer = new OwnCloudFileObserver(    localPath, 
                                                     account, 
             /// the local file was never registered to observe before
             observer = new OwnCloudFileObserver(    localPath, 
                                                     account, 
-                                                    getApplicationContext());
+                                                    getApplicationContext(),
+                                                    mHandler);
             mObserversMap.put(localPath, observer);
             Log_OC.d(TAG, "Observer added for path " + localPath);
         
             mObserversMap.put(localPath, observer);
             Log_OC.d(TAG, "Observer added for path " + localPath);