Merge branch 'develop' into refresh_folder_contents_when_browsed_into
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileObserverService.java
index 4caf8f4..8a63a6e 100644 (file)
@@ -1,10 +1,10 @@
 /* ownCloud Android client application
  *   Copyright (C) 2012 Bartek Przybylski
+ *   Copyright (C) 2012-2013 ownCloud Inc.
  *
  *   This program is free software: you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation, either version 3 of the License, or
- *   (at your option) any later version.
+ *   it under the terms of the GNU General Public License version 2,
+ *   as published by the Free Software Foundation.
  *
  *   This program is distributed in the hope that it will be useful,
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -22,15 +22,12 @@ import java.io.File;
 import java.util.HashMap;
 import java.util.Map;
 
+import com.owncloud.android.Log_OC;
 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.files.OwnCloudFileObserver.FileObserverStatusListener;
-import com.owncloud.android.operations.RemoteOperationResult;
-import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
 import com.owncloud.android.operations.SynchronizeFileOperation;
-import com.owncloud.android.ui.activity.ConflictsResolveActivity;
 import com.owncloud.android.utils.FileStorageUtils;
 
 import android.accounts.Account;
@@ -43,9 +40,8 @@ import android.content.IntentFilter;
 import android.database.Cursor;
 import android.os.Binder;
 import android.os.IBinder;
-import android.util.Log;
 
-public class FileObserverService extends Service implements FileObserverStatusListener {
+public class FileObserverService extends Service {
 
     public final static int CMD_INIT_OBSERVED_LIST = 1;
     public final static int CMD_ADD_OBSERVED_FILE = 2;
@@ -60,6 +56,9 @@ public class FileObserverService extends Service implements FileObserverStatusLi
     private static Map<String, OwnCloudFileObserver> mObserversMap;
     private static DownloadCompletedReceiverBis mDownloadReceiver;
     private IBinder mBinder = new LocalBinder();
+    
+    private String mDownloadAddedMessage;
+    private String mDownloadFinishMessage;
 
     public class LocalBinder extends Binder {
         FileObserverService getService() {
@@ -71,13 +70,18 @@ public class FileObserverService extends Service implements FileObserverStatusLi
     public void onCreate() {
         super.onCreate();
         mDownloadReceiver = new DownloadCompletedReceiverBis();
+        
+        FileDownloader downloader = new FileDownloader();
+        mDownloadAddedMessage = downloader.getDownloadAddedMessage();
+        mDownloadFinishMessage= downloader.getDownloadFinishMessage();
+        
         IntentFilter filter = new IntentFilter();
-        filter.addAction(FileDownloader.DOWNLOAD_ADDED_MESSAGE);
-        filter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE);        
+        filter.addAction(mDownloadAddedMessage);
+        filter.addAction(mDownloadFinishMessage);        
         registerReceiver(mDownloadReceiver, filter);
         
         mObserversMap = new HashMap<String, OwnCloudFileObserver>();
-        initializeObservedList();
+        //initializeObservedList();
     }
     
     
@@ -86,6 +90,7 @@ public class FileObserverService extends Service implements FileObserverStatusLi
         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");
     }
     
     
@@ -104,7 +109,7 @@ public class FileObserverService extends Service implements FileObserverStatusLi
         }
             
         if (!intent.hasExtra(KEY_FILE_CMD)) {
-            Log.e(TAG, "No KEY_FILE_CMD argument given");
+            Log_OC.e(TAG, "No KEY_FILE_CMD argument given");
             return Service.START_STICKY;
         }
 
@@ -121,7 +126,7 @@ public class FileObserverService extends Service implements FileObserverStatusLi
                                     (Account)intent.getParcelableExtra(KEY_CMD_ARG_ACCOUNT));
                 break;
             default:
-                Log.wtf(TAG, "Incorrect key given");
+                Log_OC.wtf(TAG, "Incorrect key given");
         }
 
         return Service.START_STICKY;
@@ -140,7 +145,7 @@ public class FileObserverService extends Service implements FileObserverStatusLi
                 ProviderTableMeta.FILE_KEEP_IN_SYNC + " = ?",
                 new String[] {String.valueOf(1)},
                 null);
-        if (!c.moveToFirst()) return;
+        if (c == null || !c.moveToFirst()) return;
         AccountManager acm = AccountManager.get(this);
         Account[] accounts = acm.getAccounts();
         do {
@@ -158,23 +163,24 @@ public class FileObserverService extends Service implements FileObserverStatusLi
                 continue;
 
             String path = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
+            if (path == null || path.length() <= 0)
+                continue;
             OwnCloudFileObserver observer =
-                    new OwnCloudFileObserver(path, OwnCloudFileObserver.CHANGES_ONLY);
-            observer.setContext(getApplicationContext());
-            observer.setAccount(account);
-            observer.setStorageManager(storage);
-            observer.setOCFile(storage.getFileByPath(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH))));
-            observer.addObserverStatusListener(this);
+                    new OwnCloudFileObserver(   path, 
+                                                account, 
+                                                getApplicationContext(), 
+                                                OwnCloudFileObserver.CHANGES_ONLY);
             mObserversMap.put(path, observer);
             if (new File(path).exists()) {
                 observer.startWatching();
-                Log.d(TAG, "Started watching file " + path);
+                Log_OC.d(TAG, "Started watching file " + path);
             }
             
         } while (c.moveToNext());
         c.close();
     }
     
+    
     /**
      * Registers the local copy of a remote file to be observed for local changes,
      * an automatically updated in the ownCloud server.
@@ -190,7 +196,7 @@ public class FileObserverService extends Service implements FileObserverStatusLi
      */
     private void addObservedFile(OCFile file, Account account) {
         if (file == null) {
-            Log.e(TAG, "Trying to add a NULL file to observer");
+            Log_OC.e(TAG, "Trying to add a NULL file to observer");
             return;
         }
         String localPath = file.getStoragePath();
@@ -200,23 +206,16 @@ public class FileObserverService extends Service implements FileObserverStatusLi
         OwnCloudFileObserver observer = mObserversMap.get(localPath);
         if (observer == null) {
             /// the local file was never registered to observe before
-            observer = new OwnCloudFileObserver(localPath, OwnCloudFileObserver.CHANGES_ONLY);
-            //Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
-            observer.setAccount(account);
-            FileDataStorageManager storage =
-                    new FileDataStorageManager(account, getContentResolver());  // I don't trust in this resolver's life span...
-            observer.setStorageManager(storage);
-            //observer.setOCFile(storage.getFileByLocalPath(path));   // ISSUE 10 - the fix in FileDetailsFragment to avoid path == null was not enough; it the file was never down before, this sets a NULL OCFile in the observer
-            observer.setOCFile(file);
-            observer.addObserverStatusListener(this);
-            observer.setContext(getApplicationContext());
-
+            observer = new OwnCloudFileObserver(    localPath, 
+                                                    account, 
+                                                    getApplicationContext(), 
+                                                    OwnCloudFileObserver.CHANGES_ONLY);
             mObserversMap.put(localPath, observer);
-            Log.d(TAG, "Observer added for path " + localPath);
+            Log_OC.d(TAG, "Observer added for path " + localPath);
         
             if (file.isDown()) {
                 observer.startWatching();
-                Log.d(TAG, "Started watching " + localPath);
+                Log_OC.d(TAG, "Started watching " + localPath);
             }   // else - the observance can't be started on a file not already down; mDownloadReceiver will get noticed when the download of the file finishes
         }
         
@@ -237,7 +236,7 @@ public class FileObserverService extends Service implements FileObserverStatusLi
      */
     private void removeObservedFile(OCFile file, Account account) {
         if (file == null) {
-            Log.e(TAG, "Trying to remove a NULL file");
+            Log_OC.e(TAG, "Trying to remove a NULL file");
             return;
         }
         String localPath = file.getStoragePath();
@@ -249,31 +248,11 @@ public class FileObserverService extends Service implements FileObserverStatusLi
         if (observer != null) {
             observer.stopWatching();
             mObserversMap.remove(observer);
-            Log.d(TAG, "Stopped watching " + localPath);
+            Log_OC.d(TAG, "Stopped watching " + localPath);
         }
         
     }
 
-    
-    @Override
-    public void onObservedFileStatusUpdate(String localPath, String remotePath, Account account, RemoteOperationResult result) {
-        if (!result.isSuccess()) {
-            if (result.getCode() == ResultCode.SYNC_CONFLICT) {
-                // ISSUE 5: if the user is not running the app (this is a service!), this can be very intrusive; a notification should be preferred
-                Intent i = new Intent(getApplicationContext(), ConflictsResolveActivity.class);
-                i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
-                i.putExtra("remotepath", remotePath);
-                i.putExtra("localpath", localPath);
-                i.putExtra("account", account);
-                startActivity(i);
-                
-            } else {
-                // TODO send notification to the notification bar?
-            }
-        } // else, nothing else to do; now it's duty of FileUploader service 
-    }
-    
-    
 
     /**
      *  Private receiver listening to events broadcast by the FileDownloader service.
@@ -288,14 +267,14 @@ public class FileObserverService extends Service implements FileObserverStatusLi
             String downloadPath = intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH);
             OwnCloudFileObserver observer = mObserversMap.get(downloadPath);
             if (observer != null) {
-                if (intent.getAction().equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE) &&
-                        new File(downloadPath).exists()) {  // the download could be successful, or not; in both cases, the file could be down, due to a former download or upload   
+                if (intent.getAction().equals(mDownloadFinishMessage) &&
+                        new File(downloadPath).exists()) {  // the download could be successful. not; in both cases, the file could be down, due to a former download or upload   
                     observer.startWatching();
-                    Log.d(TAG, "Watching again " + downloadPath);
+                    Log_OC.d(TAG, "Watching again " + downloadPath);
                 
-                } else if (intent.getAction().equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)) {
+                } else if (intent.getAction().equals(mDownloadAddedMessage)) {
                     observer.stopWatching();
-                    Log.d(TAG, "Disabling observance of " + downloadPath);
+                    Log_OC.d(TAG, "Disabling observance of " + downloadPath);
                 } 
             }
         }