Merge pull request #1048 from owncloud/shareWithYou_icon_in_fileList
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileDownloader.java
index 67cbc59..f6d3b50 100644 (file)
@@ -1,4 +1,6 @@
-/* ownCloud Android client application
+/**
+ *   ownCloud Android client application
+ *
  *   Copyright (C) 2012 Bartek Przybylski
  *   Copyright (C) 2012-2015 ownCloud Inc.
  *
  *   Copyright (C) 2012 Bartek Przybylski
  *   Copyright (C) 2012-2015 ownCloud Inc.
  *
@@ -25,8 +27,8 @@ 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.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.authentication.AuthenticatorActivity;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.authentication.AuthenticatorActivity;
@@ -51,7 +53,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 +70,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";
@@ -113,11 +118,16 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         super.onCreate();
         Log_OC.d(TAG, "Creating service");
         mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         super.onCreate();
         Log_OC.d(TAG, "Creating service");
         mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
-        HandlerThread thread = new HandlerThread("FileDownloaderThread", Process.THREAD_PRIORITY_BACKGROUND);
+        HandlerThread thread = new HandlerThread("FileDownloaderThread",
+                Process.THREAD_PRIORITY_BACKGROUND);
         thread.start();
         mServiceLooper = thread.getLooper();
         mServiceHandler = new ServiceHandler(mServiceLooper, this);
         mBinder = new FileDownloaderBinder();
         thread.start();
         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 +142,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();
     }
 
@@ -177,8 +192,10 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
 
                 // Store file on db with state 'downloading'
                     /*
 
                 // Store file on db with state 'downloading'
                     /*
-                    TODO - check if helps with UI responsiveness, letting only folders use FileDownloaderBinder to check
-                    FileDataStorageManager storageManager = new FileDataStorageManager(account, getContentResolver());
+                    TODO - check if helps with UI responsiveness,
+                    letting only folders use FileDownloaderBinder to check
+                    FileDataStorageManager storageManager =
+                    new FileDataStorageManager(account, getContentResolver());
                     file.setDownloading(true);
                     storageManager.saveFile(file);
                     */
                     file.setDownloading(true);
                     storageManager.saveFile(file);
                     */
@@ -224,6 +241,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.
@@ -233,7 +260,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
     public class FileDownloaderBinder extends Binder implements OnDatatransferProgressListener {
 
         /**
     public class FileDownloaderBinder extends Binder implements OnDatatransferProgressListener {
 
         /**
-         * Map of listeners that will be reported about progress of downloads from a {@link FileDownloaderBinder}
+         * Map of listeners that will be reported about progress of downloads from a
+         * {@link FileDownloaderBinder}
          * instance.
          */
         private Map<Long, OnDatatransferProgressListener> mBoundListeners =
          * instance.
          */
         private Map<Long, OnDatatransferProgressListener> mBoundListeners =
@@ -265,7 +293,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
                         mCurrentDownload.getRemotePath().startsWith(file.getRemotePath()) &&
                         account.name.equals(mCurrentAccount.name)) {
                     /*Log_OC.v(   "NOW " + TAG + ", thread " + Thread.currentThread().getName(),
                         mCurrentDownload.getRemotePath().startsWith(file.getRemotePath()) &&
                         account.name.equals(mCurrentAccount.name)) {
                     /*Log_OC.v(   "NOW " + TAG + ", thread " + Thread.currentThread().getName(),
-                            "Canceling current sync as descendant: " + mCurrentDownload.getRemotePath());*/
+                     "Canceling current sync as descendant: " + mCurrentDownload.getRemotePath());*/
                     mCurrentDownload.cancel();
                 }
             }
                     mCurrentDownload.cancel();
                 }
             }
@@ -295,8 +323,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
 
 
         /**
 
 
         /**
-         * Returns True when the file described by 'file' in the ownCloud account 'account' is downloading or
-         * waiting to download.
+         * Returns True when the file described by 'file' in the ownCloud account 'account'
+         * is downloading or waiting to download.
          * <p/>
          * If 'file' is a directory, returns 'true' if any of its descendant files is downloading or
          * waiting to download.
          * <p/>
          * If 'file' is a directory, returns 'true' if any of its descendant files is downloading or
          * waiting to download.
@@ -345,12 +373,15 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         }
 
         @Override
         }
 
         @Override
-        public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer,
-                                       String fileName) {
-            //String key = buildKey(mCurrentDownload.getAccount(), mCurrentDownload.getFile().getRemotePath());
-            OnDatatransferProgressListener boundListener = mBoundListeners.get(mCurrentDownload.getFile().getFileId());
+        public void onTransferProgress(long progressRate, long totalTransferredSoFar,
+                                       long totalToTransfer, String fileName) {
+            //String key = buildKey(mCurrentDownload.getAccount(),
+            // mCurrentDownload.getFile().getRemotePath());
+            OnDatatransferProgressListener boundListener =
+                    mBoundListeners.get(mCurrentDownload.getFile().getFileId());
             if (boundListener != null) {
             if (boundListener != null) {
-                boundListener.onTransferProgress(progressRate, totalTransferredSoFar, totalToTransfer, fileName);
+                boundListener.onTransferProgress(progressRate, totalTransferredSoFar,
+                        totalToTransfer, fileName);
             }
         }
 
             }
         }
 
@@ -374,7 +405,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
      * Created with the Looper of a new thread, started in {@link FileUploader#onCreate()}.
      */
     private static class ServiceHandler extends Handler {
      * Created with the Looper of a new thread, started in {@link FileUploader#onCreate()}.
      */
     private static class ServiceHandler extends Handler {
-        // don't make it a final class, and don't remove the static ; lint will warn about a possible memory leak
+        // don't make it a final class, and don't remove the static ; lint will warn about a
+        // possible memory leak
         FileDownloader mService;
 
         public ServiceHandler(Looper looper, FileDownloader service) {
         FileDownloader mService;
 
         public ServiceHandler(Looper looper, FileDownloader service) {
@@ -421,7 +453,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
                 RemoteOperationResult downloadResult = null;
                 try {
                     /// prepare client object to send the request to the ownCloud server
                 RemoteOperationResult downloadResult = null;
                 try {
                     /// prepare client object to send the request to the ownCloud server
-                    if (mCurrentAccount == null || !mCurrentAccount.equals(mCurrentDownload.getAccount())) {
+                    if (mCurrentAccount == null ||
+                            !mCurrentAccount.equals(mCurrentDownload.getAccount())) {
                         mCurrentAccount = mCurrentDownload.getAccount();
                         mStorageManager = new FileDataStorageManager(
                                 mCurrentAccount,
                         mCurrentAccount = mCurrentDownload.getAccount();
                         mStorageManager = new FileDataStorageManager(
                                 mCurrentAccount,
@@ -429,7 +462,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
                         );
                     }   // else, reuse storage manager from previous operation
 
                         );
                     }   // else, reuse storage manager from previous operation
 
-                    // always get client from client manager, to get fresh credentials in case of update
+                    // always get client from client manager, to get fresh credentials in case
+                    // of update
                     OwnCloudAccount ocAccount = new OwnCloudAccount(mCurrentAccount, this);
                     mDownloadClient = OwnCloudClientManagerFactory.getDefaultSingleton().
                             getClientFor(ocAccount, this);
                     OwnCloudAccount ocAccount = new OwnCloudAccount(mCurrentAccount, this);
                     mDownloadClient = OwnCloudClientManagerFactory.getDefaultSingleton().
                             getClientFor(ocAccount, this);
@@ -444,10 +478,12 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
                     }
 
                 } catch (AccountsException e) {
                     }
 
                 } catch (AccountsException e) {
-                    Log_OC.e(TAG, "Error while trying to get authorization for " + mCurrentAccount.name, e);
+                    Log_OC.e(TAG, "Error while trying to get authorization for "
+                            + mCurrentAccount.name, e);
                     downloadResult = new RemoteOperationResult(e);
                 } catch (IOException e) {
                     downloadResult = new RemoteOperationResult(e);
                 } catch (IOException e) {
-                    Log_OC.e(TAG, "Error while trying to get authorization for " + mCurrentAccount.name, e);
+                    Log_OC.e(TAG, "Error while trying to get authorization for "
+                            + mCurrentAccount.name, e);
                     downloadResult = new RemoteOperationResult(e);
 
                 } finally {
                     downloadResult = new RemoteOperationResult(e);
 
                 } finally {
@@ -455,16 +491,19 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
                         "Removing payload " + mCurrentDownload.getRemotePath());*/
 
                     Pair<DownloadFileOperation, String> removeResult =
                         "Removing payload " + mCurrentDownload.getRemotePath());*/
 
                     Pair<DownloadFileOperation, String> removeResult =
-                            mPendingDownloads.removePayload(mCurrentAccount, mCurrentDownload.getRemotePath());
+                            mPendingDownloads.removePayload(mCurrentAccount,
+                                    mCurrentDownload.getRemotePath());
 
                     /// notify result
                     notifyDownloadResult(mCurrentDownload, downloadResult);
 
 
                     /// notify result
                     notifyDownloadResult(mCurrentDownload, downloadResult);
 
-                    sendBroadcastDownloadFinished(mCurrentDownload, downloadResult, removeResult.second);
+                    sendBroadcastDownloadFinished(mCurrentDownload, downloadResult,
+                            removeResult.second);
                 }
             } else {
                 // Cancel the transfer
                 }
             } else {
                 // Cancel the transfer
-                Log_OC.d(TAG, "Account " + mCurrentDownload.getAccount().toString() + " doesn't exist");
+                Log_OC.d(TAG, "Account " + mCurrentDownload.getAccount().toString() +
+                        " doesn't exist");
                 cancelDownloadsForAccount(mCurrentDownload.getAccount());
 
             }
                 cancelDownloadsForAccount(mCurrentDownload.getAccount());
 
             }
@@ -546,7 +585,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
      * Callback method to update the progress bar in the status notification.
      */
     @Override
      * Callback method to update the progress bar in the status notification.
      */
     @Override
-    public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filePath) {
+    public void onTransferProgress(long progressRate, long totalTransferredSoFar,
+                                   long totalToTransfer, String filePath) {
         int percent = (int) (100.0 * ((double) totalTransferredSoFar) / ((double) totalToTransfer));
         if (percent != mLastPercent) {
             mNotificationBuilder.setProgress(100, percent, totalToTransfer < 0);
         int percent = (int) (100.0 * ((double) totalTransferredSoFar) / ((double) totalToTransfer));
         if (percent != mLastPercent) {
             mNotificationBuilder.setProgress(100, percent, totalToTransfer < 0);
@@ -565,7 +605,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
      * @param downloadResult Result of the download operation.
      * @param download       Finished download operation
      */
      * @param downloadResult Result of the download operation.
      * @param download       Finished download operation
      */
-    private void notifyDownloadResult(DownloadFileOperation download, RemoteOperationResult downloadResult) {
+    private void notifyDownloadResult(DownloadFileOperation download,
+                                      RemoteOperationResult downloadResult) {
         mNotificationManager.cancel(R.string.downloader_download_in_progress_ticker);
         if (!downloadResult.isCancelled()) {
             int tickerId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_ticker :
         mNotificationManager.cancel(R.string.downloader_download_in_progress_ticker);
         if (!downloadResult.isCancelled()) {
             int tickerId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_ticker :
@@ -589,16 +630,19 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
 
                 // let the user update credentials with one click
                 Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
 
                 // let the user update credentials with one click
                 Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
-                updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, download.getAccount());
+                updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT,
+                        download.getAccount());
                 updateAccountCredentials.putExtra(
                 updateAccountCredentials.putExtra(
-                        AuthenticatorActivity.EXTRA_ACTION, AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN
+                        AuthenticatorActivity.EXTRA_ACTION,
+                        AuthenticatorActivity.ACTION_UPDATE_EXPIRED_TOKEN
                 );
                 updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                 updateAccountCredentials.addFlags(Intent.FLAG_FROM_BACKGROUND);
                 mNotificationBuilder
                         .setContentIntent(PendingIntent.getActivity(
                 );
                 updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                 updateAccountCredentials.addFlags(Intent.FLAG_FROM_BACKGROUND);
                 mNotificationBuilder
                         .setContentIntent(PendingIntent.getActivity(
-                                this, (int) System.currentTimeMillis(), updateAccountCredentials, PendingIntent.FLAG_ONE_SHOT));
+                                this, (int) System.currentTimeMillis(), updateAccountCredentials,
+                                PendingIntent.FLAG_ONE_SHOT));
 
             } else {
                 // TODO put something smart in showDetailsIntent
 
             } else {
                 // TODO put something smart in showDetailsIntent
@@ -609,7 +653,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
             }
 
             mNotificationBuilder.setContentText(
             }
 
             mNotificationBuilder.setContentText(
-                    ErrorMessageAdapter.getErrorCauseMessage(downloadResult, download, getResources())
+                    ErrorMessageAdapter.getErrorCauseMessage(downloadResult, download,
+                            getResources())
             );
             mNotificationManager.notify(tickerId, mNotificationBuilder.build());
 
             );
             mNotificationManager.notify(tickerId, mNotificationBuilder.build());
 
@@ -627,7 +672,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
 
 
     /**
 
 
     /**
-     * Sends a broadcast when a download finishes in order to the interested activities can update their view
+     * Sends a broadcast when a download finishes in order to the interested activities can
+     * update their view
      *
      * @param download               Finished download operation
      * @param downloadResult         Result of the download operation
      *
      * @param download               Finished download operation
      * @param downloadResult         Result of the download operation
@@ -655,7 +701,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
      * @param download           Added download operation
      * @param linkedToRemotePath Path in the downloads tree where the download was linked to
      */
      * @param download           Added download operation
      * @param linkedToRemotePath Path in the downloads tree where the download was linked to
      */
-    private void sendBroadcastNewDownload(DownloadFileOperation download, String linkedToRemotePath) {
+    private void sendBroadcastNewDownload(DownloadFileOperation download,
+                                          String linkedToRemotePath) {
         Intent added = new Intent(getDownloadAddedMessage());
         added.putExtra(ACCOUNT_NAME, download.getAccount().name);
         added.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());
         Intent added = new Intent(getDownloadAddedMessage());
         added.putExtra(ACCOUNT_NAME, download.getAccount().name);
         added.putExtra(EXTRA_REMOTE_PATH, download.getRemotePath());
@@ -667,26 +714,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);
     }
 }
     }
 }