Context handling made consistent
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileObserverService.java
index dc533d1..41e4e52 100644 (file)
@@ -1,5 +1,24 @@
+/* ownCloud Android client application
+ *   Copyright (C) 2012 Bartek Przybylski
+ *
+ *   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.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
 package com.owncloud.android.files.services;
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -32,7 +51,7 @@ public class FileObserverService extends Service implements FileObserverStatusLi
     public final static int CMD_DEL_OBSERVED_FILE = 3;
     public final static int CMD_ADD_DOWNLOADING_FILE = 4;
 
-    private static String TAG = "FileObserverService";
+    private static String TAG = FileObserverService.class.getSimpleName();
     private static List<OwnCloudFileObserver> mObservers;
     private static List<DownloadCompletedReceiver> mDownloadReceivers;
     private static Object mReceiverListLock = new Object();
@@ -139,12 +158,12 @@ public class FileObserverService extends Service implements FileObserverStatusLi
             observer = mObservers.get(i);
             if (observer.getPath().equals(path))
                 duplicate = true;
-            observer.setContext(getBaseContext());
+            observer.setContext(getApplicationContext());
         }
         if (duplicate) return;
         observer = new OwnCloudFileObserver(path, OwnCloudFileObserver.CHANGES_ONLY);
-        observer.setContext(getBaseContext());
-        Account account = AccountUtils.getCurrentOwnCloudAccount(getBaseContext());
+        observer.setContext(getApplicationContext());
+        Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
         observer.setAccount(account);
         FileDataStorageManager storage =
                 new FileDataStorageManager(account, getContentResolver());
@@ -163,17 +182,16 @@ public class FileObserverService extends Service implements FileObserverStatusLi
         if (path == null) return;
         if (mObservers == null) {
             initializeObservedList();
-            return;
         }
         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) {
@@ -211,6 +229,7 @@ public class FileObserverService extends Service implements FileObserverStatusLi
         switch (status) {
             case 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);
@@ -240,11 +259,15 @@ public class FileObserverService extends Service implements FileObserverStatusLi
         @Override
         public void onReceive(Context context, Intent intent) {
             if (mPath.equals(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH))) {
-                context.unregisterReceiver(this);
-                removeReceiverFromList(this);
-                mObserver.startWatching();
-                Log.d(TAG, "Started watching " + mPath);
-                return;
+                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 
             }
         }