Try to cancel transfers when an account is deleted outside of the app: Done
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileUploader.java
index f9c57cc..3ce1aac 100644 (file)
@@ -1,6 +1,6 @@
 /* ownCloud Android client application
  *   Copyright (C) 2012 Bartek Przybylski
- *   Copyright (C) 2012-2013 ownCloud Inc.
+ *   Copyright (C) 2012-2015 ownCloud Inc.
  *
  *   This program is free software: you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License version 2,
@@ -56,6 +56,7 @@ import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
+import com.owncloud.android.lib.common.operations.OperationCancelledException;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
@@ -211,11 +212,11 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
 
             if (intent.hasExtra(KEY_FILE)) {
                 files = (OCFile[]) intent.getParcelableArrayExtra(KEY_FILE); // TODO
-                // will
-                // this
-                // casting
-                // work
-                // fine?
+                                                                             // will
+                                                                             // this
+                                                                             // casting
+                                                                             // work
+                                                                             // fine?
 
             } else {
                 localPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE);
@@ -327,7 +328,6 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
         return false;   // not accepting rebinding (default behaviour)
     }
 
-
     /**
      * Binder to let client components to perform operations on the queue of
      * uploads.
@@ -353,19 +353,42 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
                 upload = mPendingUploads.remove(buildRemoteName(account, file));
             }
             if (upload != null) {
-                upload.cancel();
+                mCurrentUpload.cancel();
             }
         }
 
+        /**
+         * Cancels a pending or current upload for an account
+         *
+         * @param account Owncloud accountName where the remote file will be stored.
+         */
+        public void cancel(Account account) {
+            Log_OC.d(TAG, "Account= " + account.name);
 
+            if (mCurrentUpload != null) {
+                Log_OC.d(TAG, "Current Upload Account= " + mCurrentUpload.getAccount().name);
+                if (mCurrentUpload.getAccount().name.equals(account.name)) {
+                    mCurrentUpload.cancel();
+                }
+            }
+            // Cancel pending uploads
+            Iterator<String> it = mPendingUploads.keySet().iterator();
+            Log_OC.d(TAG, "Number of pending uploads= "  + mPendingUploads.size());
+            while (it.hasNext()) {
+                String key = it.next();
+                Log_OC.d(TAG, "mPendingUploads CANCELLED " + key);
+                if (key.startsWith(account.name)) {
+                    synchronized (mPendingUploads) {
+                        mPendingUploads.remove(key);
+                    }
+                }
+            }
+        }
 
         public void clearListeners() {
             mBoundListeners.clear();
         }
 
-
-
-
         /**
          * Returns True when the file described by 'file' is being uploaded to
          * the ownCloud account 'account' or waiting for it
@@ -436,6 +459,16 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
             }
         }
 
+        /**
+         * Review uploads and cancel it if its account doesn't exist
+         */
+        public void reviewUploads() {
+            if (mCurrentUpload != null &&
+                    !AccountUtils.exists(mCurrentUpload.getAccount(), getApplicationContext())) {
+                mCurrentUpload.cancel();
+            }
+            // The rest of uploads are cancelled when they try to start
+        }
     }
 
     /**
@@ -548,6 +581,8 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
             } else {
                 // Cancel the transfer
                 Log_OC.d(TAG, "Account " + mCurrentUpload.getAccount().toString() + " doesn't exist");
+                cancelUploadForAccount(mCurrentUpload.getAccount().name);
+
             }
         }
 
@@ -566,7 +601,8 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
     private RemoteOperationResult grantFolderExistence(String pathToGrant) {
         RemoteOperation operation = new ExistenceCheckRemoteOperation(pathToGrant, this, false);
         RemoteOperationResult result = operation.execute(mUploadClient);
-        if (!result.isSuccess() && result.getCode() == ResultCode.FILE_NOT_FOUND && mCurrentUpload.isRemoteFolderToBeCreated()) {
+        if (!result.isSuccess() && result.getCode() == ResultCode.FILE_NOT_FOUND &&
+                mCurrentUpload.isRemoteFolderToBeCreated()) {
             SyncOperation syncOp = new CreateFolderOperation( pathToGrant, true);
             result = syncOp.execute(mUploadClient, mStorageManager);
         }
@@ -587,7 +623,8 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
 
     private OCFile createLocalFolder(String remotePath) {
         String parentPath = new File(remotePath).getParent();
-        parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
+        parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ?
+                parentPath : parentPath + OCFile.PATH_SEPARATOR;
         OCFile parent = mStorageManager.getFileByPath(parentPath);
         if (parent == null) {
             parent = createLocalFolder(parentPath);
@@ -887,4 +924,22 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
                 !localPath.endsWith(FILE_EXTENSION_PDF);
     }
 
+    /**
+     * Remove uploads of an account
+     * @param accountName
+     */
+    private void cancelUploadForAccount(String accountName){
+        // this can be slow if there are many uploads :(
+        Iterator<String> it = mPendingUploads.keySet().iterator();
+        Log_OC.d(TAG, "Number of pending updloads= "  + mPendingUploads.size());
+        while (it.hasNext()) {
+            String key = it.next();
+            Log_OC.d(TAG, "mPendingUploads CANCELLED " + key);
+            if (key.startsWith(accountName)) {
+                synchronized (mPendingUploads) {
+                    mPendingUploads.remove(key);
+                }
+            }
+        }
+    }
 }