Uploading files are protected against removal from other apps
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileUploader.java
index 298c93b..f7340f6 100644 (file)
@@ -42,6 +42,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
     public static final String KEY_REMOTE_FILE = "REMOTE_FILE";
     public static final String KEY_ACCOUNT = "ACCOUNT";
     public static final String KEY_UPLOAD_TYPE = "UPLOAD_TYPE";
+    public static final String KEY_FORCE_OVERWRITE = "KEY_FORCE_OVERWRITE";
     public static final String ACCOUNT_NAME = "ACCOUNT_NAME";    
     public static final String KEY_MIME_TYPE = "MIME_TYPE";
     public static final String KEY_INSTANT_UPLOAD = "INSTANT_UPLOAD";
@@ -97,7 +98,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
 
         @Override
         public void handleMessage(Message msg) {
-            uploadFile();
+            uploadFile(msg.arg2==1?true:false);
             stopSelf(msg.arg1);
         }
     }
@@ -146,6 +147,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
                 
         Message msg = mServiceHandler.obtainMessage();
         msg.arg1 = startId;
+        msg.arg2 = intent.getBooleanExtra(KEY_FORCE_OVERWRITE, false)?1:0;
         mServiceHandler.sendMessage(msg);
 
         return Service.START_NOT_STICKY;
@@ -155,7 +157,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
     /**
      * Core upload method: sends the file(s) to upload
      */
-    public void uploadFile() {
+    public void uploadFile(boolean force_override) {
         FileDataStorageManager storageManager = new FileDataStorageManager(mAccount, getContentResolver());
 
         mTotalDataToSend = mSendData = mPreviousPercent = 0;
@@ -213,9 +215,12 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
             mCurrentIndexUpload = i;
             long parentDirId = -1;
             boolean uploadResult = false;
-            String availablePath = getAvailableRemotePath(wc, mRemotePaths[i]);
+            String availablePath = mRemotePaths[i];
+            if (!force_override)
+                availablePath = getAvailableRemotePath(wc, mRemotePaths[i]);
             try {
                 File f = new File(mRemotePaths[i]);
+                long size = localFiles[i].length();
                 parentDirId = storageManager.getFileByPath(f.getParent().endsWith("/")?f.getParent():f.getParent()+"/").getFileId();
                 if(availablePath != null) {
                     mRemotePaths[i] = availablePath;
@@ -223,11 +228,13 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
                     if (wc.putFile(mLocalPaths[i], mRemotePaths[i], mimeType)) {
                         OCFile new_file = new OCFile(mRemotePaths[i]);
                         new_file.setMimetype(mimeType);
-                        new_file.setFileLength(localFiles[i].length());
+                        new_file.setFileLength(size);
                         new_file.setModificationTimestamp(System.currentTimeMillis());
                         new_file.setLastSyncDate(0);
                         new_file.setStoragePath(mLocalPaths[i]);         
                         new_file.setParentId(parentDirId);
+                        if (force_override)
+                            new_file.setKeepInSync(true);
                         storageManager.saveFile(new_file);
                         mSuccessCounter++;
                         uploadResult = true;