X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/83ad608faadfcfc5a0ceee9d6c460702b3ff3f11..01fc29c892bf5d1cb7dc1f8094d91a1a6a96a6bf:/src/com/owncloud/android/files/services/FileDownloader.java diff --git a/src/com/owncloud/android/files/services/FileDownloader.java b/src/com/owncloud/android/files/services/FileDownloader.java index 67cbc599..c76ab79d 100644 --- a/src/com/owncloud/android/files/services/FileDownloader.java +++ b/src/com/owncloud/android/files/services/FileDownloader.java @@ -25,7 +25,6 @@ import java.util.HashMap; 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; @@ -51,7 +50,9 @@ import com.owncloud.android.ui.preview.PreviewImageFragment; import com.owncloud.android.utils.ErrorMessageAdapter; import android.accounts.Account; +import android.accounts.AccountManager; import android.accounts.AccountsException; +import android.accounts.OnAccountsUpdateListener; 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; -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"; @@ -118,6 +120,10 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis 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; + + // remove AccountsUpdatedListener + AccountManager am = AccountManager.get(getApplicationContext()); + am.removeOnAccountsUpdatedListener(this); + super.onDestroy(); } @@ -224,6 +235,16 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis 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. @@ -667,26 +688,10 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis /** * Remove downloads of an account * - * @param account + * @param account Downloads account to remove */ private void cancelDownloadsForAccount(Account account) { // Cancel pending downloads - ConcurrentMap downloadsAccount = mPendingDownloads.get(account); - Iterator 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); } }