Grant that a favourite file in the device is watched if a redownload fails
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileObserverService.java
index 2cae6f2..70b6b21 100644 (file)
@@ -18,6 +18,7 @@
 
 package com.owncloud.android.files.services;
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -181,17 +182,16 @@ public class FileObserverService extends Service implements FileObserverStatusLi
         if (path == null) return;
         if (mObservers == null) {
             initializeObservedList();
-            return; // ISSUE 2: why return? ; the file still has to be removed of the mObservers !
         }
         for (int i = 0; i < mObservers.size(); ++i) {
             OwnCloudFileObserver observer = mObservers.get(i);
             if (observer.getPath().equals(path)) {
                 observer.stopWatching();
                 mObservers.remove(i);
+                Log.d(TAG, "Stopped watching " + path);
                 break;
             }
         }
-        Log.d(TAG, "Stopped watching " + path);
     }
         
     private void addDownloadingFile(String remotePath) {
@@ -258,12 +258,16 @@ public class FileObserverService extends Service implements FileObserverStatusLi
         
         @Override
         public void onReceive(Context context, Intent intent) {
-            if (mPath.equals(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH))) {  // ISSUE 3: this condition will be false if the download failed; in that case, the download won't ever be retried
-                context.unregisterReceiver(this);
-                removeReceiverFromList(this);
-                mObserver.startWatching();
-                Log.d(TAG, "Started watching " + mPath);
-                return;
+            if (mPath.equals(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH))) {
+                if ((new File(mPath)).exists()) {   
+                    // the download could be successful, or not; in both cases, the file could be down, due to a former download or upload
+                    context.unregisterReceiver(this);
+                    removeReceiverFromList(this);
+                    mObserver.startWatching();
+                    Log.d(TAG, "Started watching " + mPath);
+                    return;
+                }   // else - keep waiting for a future retry of the download ; 
+                    // mObserver.startWatching() won't ever work if the file is not in the device when it's called 
             }
         }