Bug fixed: Upload fails
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileDownloader.java
index 67cbc59..c76ab79 100644 (file)
@@ -25,7 +25,6 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Vector;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Vector;
-import java.util.concurrent.ConcurrentMap;
 
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
 
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
@@ -51,7 +50,9 @@ import com.owncloud.android.ui.preview.PreviewImageFragment;
 import com.owncloud.android.utils.ErrorMessageAdapter;
 
 import android.accounts.Account;
 import com.owncloud.android.utils.ErrorMessageAdapter;
 
 import android.accounts.Account;
+import android.accounts.AccountManager;
 import android.accounts.AccountsException;
 import android.accounts.AccountsException;
+import android.accounts.OnAccountsUpdateListener;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.app.Service;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.app.Service;
@@ -66,7 +67,8 @@ import android.os.Process;
 import android.support.v4.app.NotificationCompat;
 import android.util.Pair;
 
 import android.support.v4.app.NotificationCompat;
 import android.util.Pair;
 
-public class FileDownloader extends Service implements OnDatatransferProgressListener {
+public class FileDownloader extends Service
+        implements OnDatatransferProgressListener, OnAccountsUpdateListener {
 
     public static final String EXTRA_ACCOUNT = "ACCOUNT";
     public static final String EXTRA_FILE = "FILE";
 
     public static final String EXTRA_ACCOUNT = "ACCOUNT";
     public static final String EXTRA_FILE = "FILE";
@@ -118,6 +120,10 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         mServiceLooper = thread.getLooper();
         mServiceHandler = new ServiceHandler(mServiceLooper, this);
         mBinder = new FileDownloaderBinder();
         mServiceLooper = thread.getLooper();
         mServiceHandler = new ServiceHandler(mServiceLooper, this);
         mBinder = new FileDownloaderBinder();
+
+        // add AccountsUpdatedListener
+        AccountManager am = AccountManager.get(getApplicationContext());
+        am.addOnAccountsUpdatedListener(this, null, false);
     }
 
 
     }
 
 
@@ -132,6 +138,11 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         mServiceLooper.quit();
         mServiceLooper = null;
         mNotificationManager = null;
         mServiceLooper.quit();
         mServiceLooper = null;
         mNotificationManager = null;
+
+        // remove AccountsUpdatedListener
+        AccountManager am = AccountManager.get(getApplicationContext());
+        am.removeOnAccountsUpdatedListener(this);
+
         super.onDestroy();
     }
 
         super.onDestroy();
     }
 
@@ -224,6 +235,16 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         return false;   // not accepting rebinding (default behaviour)
     }
 
         return false;   // not accepting rebinding (default behaviour)
     }
 
+    @Override
+    public void onAccountsUpdated(Account[] accounts) {
+         //review the current download and cancel it if its account doesn't exist
+        if (mCurrentDownload != null &&
+                !AccountUtils.exists(mCurrentDownload.getAccount(), getApplicationContext())) {
+            mCurrentDownload.cancel();
+        }
+        // The rest of downloads are cancelled when they try to start
+    }
+
 
     /**
      * Binder to let client components to perform operations on the queue of downloads.
 
     /**
      * Binder to let client components to perform operations on the queue of downloads.
@@ -667,26 +688,10 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
     /**
      * Remove downloads of an account
      *
     /**
      * Remove downloads of an account
      *
-     * @param account
+     * @param account       Downloads account to remove
      */
     private void cancelDownloadsForAccount(Account account) {
         // Cancel pending downloads
      */
     private void cancelDownloadsForAccount(Account account) {
         // Cancel pending downloads
-        ConcurrentMap downloadsAccount = mPendingDownloads.get(account);
-        Iterator<String> it = downloadsAccount.keySet().iterator();
-        Log_OC.d(TAG, "Number of pending downloads= " + downloadsAccount.size());
-        DownloadFileOperation download;
-        while (it.hasNext()) {
-            String key = it.next();
-            Log_OC.d(TAG, "download CANCELLED " + key);
-            if (key.startsWith(account.name)) {
-                synchronized (mPendingDownloads) {
-                    download = mPendingDownloads.get(key);
-                    if (download != null) {
-                        String remotePath = download.getRemotePath();
-                        mPendingDownloads.remove(account, remotePath);
-                    }
-                }
-            }
-        }
+        mPendingDownloads.remove(account);
     }
 }
     }
 }