Changes from Comments in PR#886
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileUploader.java
index c5b9217..d6ade1a 100644 (file)
@@ -162,7 +162,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
     @Override
     public void onCreate() {
         super.onCreate();
-        Log_OC.i(TAG, "mPendingUploads size:" + mPendingUploads.size());
+        Log_OC.d(TAG, "Creating service");
         mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         HandlerThread thread = new HandlerThread("FileUploaderThread", Process.THREAD_PRIORITY_BACKGROUND);
         thread.start();
@@ -172,6 +172,21 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
     }
 
     /**
+     * Service clean up
+     */
+    @Override
+    public void onDestroy() {
+        Log_OC.v(TAG, "Destroying service" );
+        mBinder = null;
+        mServiceHandler = null;
+        mServiceLooper.quit();
+        mServiceLooper = null;
+        mNotificationManager = null;
+        super.onDestroy();
+    }
+
+
+    /**
      * Entry point to add one or several files to the queue of uploads.
      *
      * New uploads are added calling to startService(), resulting in a call to
@@ -180,6 +195,8 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
      */
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
+        Log_OC.d(TAG, "Starting command with id " + startId);
+
         if (!intent.hasExtra(KEY_ACCOUNT) || !intent.hasExtra(KEY_UPLOAD_TYPE)
                 || !(intent.hasExtra(KEY_LOCAL_FILE) || intent.hasExtra(KEY_FILE))) {
             Log_OC.e(TAG, "Not enough information provided in intent");
@@ -353,7 +370,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
                 upload = mPendingUploads.remove(buildRemoteName(account, file));
             }
             if (upload != null) {
-                mCurrentUpload.cancel();
+                upload.cancel();
             }
         }
 
@@ -367,26 +384,12 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
 
             if (mCurrentUpload != null) {
                 Log_OC.d(TAG, "Current Upload Account= " + mCurrentUpload.getAccount().name);
-                if (mCurrentUpload.getAccount().name == account.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 updloads= "  + mPendingUploads.size());
-            while (it.hasNext()) {
-                String key = it.next();
-                Log_OC.d(TAG, "mPendingUploads CANCELLED " + key);
-                if (key.startsWith(account.name)) {
-                    UploadFileOperation upload;
-                    synchronized (mPendingUploads) {
-                        upload = mPendingUploads.remove(key);
-                    }
-                    if (upload != null) {
-                        mCurrentUpload.cancel();
-                    }
-                }
-            }
+            cancelUploadForAccount(account.name);
         }
 
         public void clearListeners() {
@@ -463,6 +466,16 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
             }
         }
 
+        /**
+         * Review uploads and cancel it if its account doesn't exist
+         */
+        public void checkAccountOfCurrentUpload() {
+            if (mCurrentUpload != null &&
+                    !AccountUtils.exists(mCurrentUpload.getAccount(), getApplicationContext())) {
+                mCurrentUpload.cancel();
+            }
+            // The rest of uploads are cancelled when they try to start
+        }
     }
 
     /**
@@ -494,6 +507,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
                     mService.uploadFile(it.next());
                 }
             }
+            Log_OC.d(TAG, "Stopping command after id " + msg.arg1);
             mService.stopSelf(msg.arg1);
         }
     }
@@ -514,7 +528,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
 
             // 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);
 
@@ -533,7 +547,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();
-                    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