Bug fixed: Upload fails
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileUploader.java
index ef8bda4..8a04be3 100644 (file)
@@ -31,6 +31,7 @@ import java.util.concurrent.ConcurrentMap;
 import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.accounts.AccountsException;
 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.app.NotificationManager;
 import android.app.PendingIntent;
 import android.app.Service;
@@ -56,7 +57,6 @@ 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.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;
 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;
@@ -77,8 +77,8 @@ import com.owncloud.android.utils.ErrorMessageAdapter;
 import com.owncloud.android.utils.UriUtils;
 
 
 import com.owncloud.android.utils.UriUtils;
 
 
-
-public class FileUploader extends Service implements OnDatatransferProgressListener {
+public class FileUploader extends Service
+        implements OnDatatransferProgressListener, OnAccountsUpdateListener {
 
     private static final String UPLOAD_FINISH_MESSAGE = "UPLOAD_FINISH";
     public static final String EXTRA_UPLOAD_RESULT = "RESULT";
 
     private static final String UPLOAD_FINISH_MESSAGE = "UPLOAD_FINISH";
     public static final String EXTRA_UPLOAD_RESULT = "RESULT";
@@ -169,6 +169,10 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
         mServiceLooper = thread.getLooper();
         mServiceHandler = new ServiceHandler(mServiceLooper, this);
         mBinder = new FileUploaderBinder();
         mServiceLooper = thread.getLooper();
         mServiceHandler = new ServiceHandler(mServiceLooper, this);
         mBinder = new FileUploaderBinder();
+
+        // add AccountsUpdatedListener
+        AccountManager am = AccountManager.get(getApplicationContext());
+        am.addOnAccountsUpdatedListener(this, null, false);
     }
 
     /**
     }
 
     /**
@@ -182,6 +186,11 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
         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();
     }
 
@@ -345,6 +354,16 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
         return false;   // not accepting rebinding (default behaviour)
     }
 
         return false;   // not accepting rebinding (default behaviour)
     }
 
+    @Override
+    public void onAccountsUpdated(Account[] accounts) {
+        // Review current upload, and cancel it if its account doen't exist
+        if (mCurrentUpload != null &&
+                !AccountUtils.exists(mCurrentUpload.getAccount(), getApplicationContext())) {
+            mCurrentUpload.cancel();
+        }
+        // The rest of uploads are cancelled when they try to start
+    }
+
     /**
      * Binder to let client components to perform operations on the queue of
      * uploads.
     /**
      * Binder to let client components to perform operations on the queue of
      * uploads.
@@ -370,7 +389,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
                 upload = mPendingUploads.remove(buildRemoteName(account, file));
             }
             if (upload != null) {
                 upload = mPendingUploads.remove(buildRemoteName(account, file));
             }
             if (upload != null) {
-                mCurrentUpload.cancel();
+                upload.cancel();
             }
         }
 
             }
         }
 
@@ -389,17 +408,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
                 }
             }
             // Cancel pending uploads
                 }
             }
             // 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);
-                    }
-                }
-            }
+            cancelUploadForAccount(account.name);
         }
 
         public void clearListeners() {
         }
 
         public void clearListeners() {
@@ -479,7 +488,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
         /**
          * Review uploads and cancel it if its account doesn't exist
          */
         /**
          * Review uploads and cancel it if its account doesn't exist
          */
-        public void reviewUploads() {
+        public void checkAccountOfCurrentUpload() {
             if (mCurrentUpload != null &&
                     !AccountUtils.exists(mCurrentUpload.getAccount(), getApplicationContext())) {
                 mCurrentUpload.cancel();
             if (mCurrentUpload != null &&
                     !AccountUtils.exists(mCurrentUpload.getAccount(), getApplicationContext())) {
                 mCurrentUpload.cancel();
@@ -538,7 +547,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
 
             // Detect if the account exists
             if (AccountUtils.exists(mCurrentUpload.getAccount(), getApplicationContext())) {
 
             // Detect if the account exists
             if (AccountUtils.exists(mCurrentUpload.getAccount(), getApplicationContext())) {
-                Log_OC.d(TAG, "Account " + mCurrentUpload.getAccount().toString() + " exists");
+                Log_OC.d(TAG, "Account " + mCurrentUpload.getAccount().name + " exists");
 
                 notifyUploadStart(mCurrentUpload);
 
 
                 notifyUploadStart(mCurrentUpload);
 
@@ -557,7 +566,8 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
 
                     /// check the existence of the parent folder for the file to upload
                     String remoteParentPath = new File(mCurrentUpload.getRemotePath()).getParent();
 
                     /// check the existence of the parent folder for the file to upload
                     String remoteParentPath = new File(mCurrentUpload.getRemotePath()).getParent();
-                    remoteParentPath = remoteParentPath.endsWith(OCFile.PATH_SEPARATOR) ? remoteParentPath : remoteParentPath + OCFile.PATH_SEPARATOR;
+                    remoteParentPath = remoteParentPath.endsWith(OCFile.PATH_SEPARATOR) ?
+                            remoteParentPath : remoteParentPath + OCFile.PATH_SEPARATOR;
                     grantResult = grantFolderExistence(remoteParentPath);
 
                     /// perform the upload
                     grantResult = grantFolderExistence(remoteParentPath);
 
                     /// perform the upload