Merge remote-tracking branch 'remotes/upstream/video_thumbnail' into beta
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileUploader.java
index df10f01..7937f40 100644 (file)
@@ -98,6 +98,8 @@ public class FileUploader extends Service
     public static final String KEY_INSTANT_UPLOAD = "INSTANT_UPLOAD";
     public static final String KEY_LOCAL_BEHAVIOUR = "BEHAVIOUR";
 
     public static final String KEY_INSTANT_UPLOAD = "INSTANT_UPLOAD";
     public static final String KEY_LOCAL_BEHAVIOUR = "BEHAVIOUR";
 
+    public static final String KEY_CANCEL_ALL = "CANCEL_ALL";
+
     public static final int LOCAL_BEHAVIOUR_COPY = 0;
     public static final int LOCAL_BEHAVIOUR_MOVE = 1;
     public static final int LOCAL_BEHAVIOUR_FORGET = 2;
     public static final int LOCAL_BEHAVIOUR_COPY = 0;
     public static final int LOCAL_BEHAVIOUR_MOVE = 1;
     public static final int LOCAL_BEHAVIOUR_FORGET = 2;
@@ -194,6 +196,21 @@ public class FileUploader extends Service
     public int onStartCommand(Intent intent, int flags, int startId) {
         Log_OC.d(TAG, "Starting command with id " + startId);
 
     public int onStartCommand(Intent intent, int flags, int startId) {
         Log_OC.d(TAG, "Starting command with id " + startId);
 
+        if (intent.hasExtra(KEY_CANCEL_ALL) && intent.hasExtra(KEY_ACCOUNT)){
+            Account account = intent.getParcelableExtra(KEY_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
+            cancelUploadsForAccount(account);
+        }
+
         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");
         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");
@@ -244,7 +261,7 @@ public class FileUploader extends Service
 
         boolean forceOverwrite = intent.getBooleanExtra(KEY_FORCE_OVERWRITE, false);
         boolean isInstant = intent.getBooleanExtra(KEY_INSTANT_UPLOAD, false);
 
         boolean forceOverwrite = intent.getBooleanExtra(KEY_FORCE_OVERWRITE, false);
         boolean isInstant = intent.getBooleanExtra(KEY_INSTANT_UPLOAD, false);
-        int localAction = intent.getIntExtra(KEY_LOCAL_BEHAVIOUR, LOCAL_BEHAVIOUR_COPY);
+        int localAction = intent.getIntExtra(KEY_LOCAL_BEHAVIOUR, LOCAL_BEHAVIOUR_FORGET);
 
         if (intent.hasExtra(KEY_FILE) && files == null) {
             Log_OC.e(TAG, "Incorrect array for OCFiles provided in upload intent");
 
         if (intent.hasExtra(KEY_FILE) && files == null) {
             Log_OC.e(TAG, "Incorrect array for OCFiles provided in upload intent");
@@ -299,8 +316,10 @@ public class FileUploader extends Service
                 Pair<String, String> putResult = mPendingUploads.putIfAbsent(
                         account, files[i].getRemotePath(), newUpload
                 );
                 Pair<String, String> putResult = mPendingUploads.putIfAbsent(
                         account, files[i].getRemotePath(), newUpload
                 );
-                uploadKey = putResult.first;
-                requestedUploads.add(uploadKey);
+                if (putResult != null) {
+                    uploadKey = putResult.first;
+                    requestedUploads.add(uploadKey);
+                }   // else, file already in the queue of uploads; don't repeat the request
             }
 
         } catch (IllegalArgumentException e) {
             }
 
         } catch (IllegalArgumentException e) {
@@ -532,8 +551,6 @@ public class FileUploader extends Service
      */
     public void uploadFile(String uploadKey) {
 
      */
     public void uploadFile(String uploadKey) {
 
-        Log_OC.v(   "NOW " + TAG + ", thread " + Thread.currentThread().getName(),
-                "Getting upload of " + uploadKey);
         mCurrentUpload = mPendingUploads.get(uploadKey);
 
         if (mCurrentUpload != null) {
         mCurrentUpload = mPendingUploads.get(uploadKey);
 
         if (mCurrentUpload != null) {
@@ -569,13 +586,15 @@ public class FileUploader extends Service
 
                     /// perform the upload
                     if (grantResult.isSuccess()) {
 
                     /// perform the upload
                     if (grantResult.isSuccess()) {
-                        Log_OC.v(   "NOW " + TAG + ", thread " + Thread.currentThread().getName(),
-                                "Executing upload of " + mCurrentUpload.getRemotePath());
                         OCFile parent = mStorageManager.getFileByPath(remoteParentPath);
                         mCurrentUpload.getFile().setParentId(parent.getFileId());
                         uploadResult = mCurrentUpload.execute(mUploadClient);
                         if (uploadResult.isSuccess()) {
                             saveUploadedFile();
                         OCFile parent = mStorageManager.getFileByPath(remoteParentPath);
                         mCurrentUpload.getFile().setParentId(parent.getFileId());
                         uploadResult = mCurrentUpload.execute(mUploadClient);
                         if (uploadResult.isSuccess()) {
                             saveUploadedFile();
+
+                        } else if (uploadResult.getCode() == ResultCode.SYNC_CONFLICT) {
+                            mStorageManager.saveConflict(mCurrentUpload.getFile(),
+                                    mCurrentUpload.getFile().getEtagInConflict());
                         }
                     } else {
                         uploadResult = grantResult;
                         }
                     } else {
                         uploadResult = grantResult;
@@ -586,8 +605,6 @@ public class FileUploader extends Service
                     uploadResult = new RemoteOperationResult(e);
 
                 } finally {
                     uploadResult = new RemoteOperationResult(e);
 
                 } finally {
-                    Log_OC.v("NOW " + TAG + ", thread " + Thread.currentThread().getName(),
-                            "Removing payload " + mCurrentUpload.getRemotePath());
                     Pair<UploadFileOperation, String> removeResult;
                     if (mCurrentUpload.wasRenamed()) {
                         removeResult = mPendingUploads.removePayload(
                     Pair<UploadFileOperation, String> removeResult;
                     if (mCurrentUpload.wasRenamed()) {
                         removeResult = mPendingUploads.removePayload(
@@ -707,7 +724,7 @@ public class FileUploader extends Service
             if (oldFile.fileExists()) {
                 oldFile.setStoragePath(null);
                 mStorageManager.saveFile(oldFile);
             if (oldFile.fileExists()) {
                 oldFile.setStoragePath(null);
                 mStorageManager.saveFile(oldFile);
-                mStorageManager.saveConflict(oldFile, false);
+                mStorageManager.saveConflict(oldFile, null);
 
             } // else: it was just an automatic renaming due to a name
             // coincidence; nothing else is needed, the storagePath is right
 
             } // else: it was just an automatic renaming due to a name
             // coincidence; nothing else is needed, the storagePath is right
@@ -715,7 +732,10 @@ public class FileUploader extends Service
         }
         file.setNeedsUpdateThumbnail(true);
         mStorageManager.saveFile(file);
         }
         file.setNeedsUpdateThumbnail(true);
         mStorageManager.saveFile(file);
-        mStorageManager.saveConflict(file, false);
+        mStorageManager.saveConflict(file, null);
+        
+        mStorageManager.triggerMediaScan(file.getStoragePath());
+
     }
 
     private void updateOCFile(OCFile file, RemoteFile remoteFile) {
     }
 
     private void updateOCFile(OCFile file, RemoteFile remoteFile) {