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.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;
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";
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper, this);
mBinder = new FileDownloaderBinder();
+
+ // add AccountsUpdatedListener
+ AccountManager am = AccountManager.get(getApplicationContext());
+ am.addOnAccountsUpdatedListener(this, null, false);
}
mServiceLooper.quit();
mServiceLooper = null;
mNotificationManager = null;
+
+ // remove AccountsUpdatedListener
+ AccountManager am = AccountManager.get(getApplicationContext());
+ am.removeOnAccountsUpdatedListener(this);
+
super.onDestroy();
}
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.
/**
* 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<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);
}
}