Merge pull request #508 from owncloud/vanish_notifications
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / FileOperationsHelper.java
index 022cbec..c473c0f 100644 (file)
@@ -19,6 +19,7 @@ package com.owncloud.android.files;
 
 import org.apache.http.protocol.HTTP;
 
 
 import org.apache.http.protocol.HTTP;
 
+import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.content.Intent;
 import android.net.Uri;
 import android.accounts.AccountManager;
 import android.content.Intent;
 import android.net.Uri;
@@ -28,6 +29,8 @@ import android.widget.Toast;
 
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
 
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
+import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 
 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
 import com.owncloud.android.lib.common.network.WebdavUtils;
 
 import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
 import com.owncloud.android.lib.common.network.WebdavUtils;
@@ -49,6 +52,9 @@ public class FileOperationsHelper {
     private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG"; 
 
     protected FileActivity mFileActivity = null;
     private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG"; 
 
     protected FileActivity mFileActivity = null;
+
+    /// Identifier of operation in progress which result shouldn't be lost 
+    private long mWaitingForOpId = Long.MAX_VALUE;
     
     public FileOperationsHelper(FileActivity fileActivity) {
         mFileActivity = fileActivity;
     
     public FileOperationsHelper(FileActivity fileActivity) {
         mFileActivity = fileActivity;
@@ -121,7 +127,7 @@ public class FileOperationsHelper {
             service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
             service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
             service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
             service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
             service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
             service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
-            mFileActivity.getOperationsServiceBinder().newOperation(service);
+            mWaitingForOpId = mFileActivity.getOperationsServiceBinder().newOperation(service);
             
         } else {
             Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
             
         } else {
             Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
@@ -159,7 +165,7 @@ public class FileOperationsHelper {
             service.setAction(OperationsService.ACTION_UNSHARE);
             service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
             service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
             service.setAction(OperationsService.ACTION_UNSHARE);
             service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
             service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
-            mFileActivity.getOperationsServiceBinder().newOperation(service);
+            mWaitingForOpId = mFileActivity.getOperationsServiceBinder().newOperation(service);
             
             mFileActivity.showLoadingDialog();
             
             
             mFileActivity.showLoadingDialog();
             
@@ -197,7 +203,7 @@ public class FileOperationsHelper {
         service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
         service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath()); 
         service.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
         service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
         service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath()); 
         service.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
-        mFileActivity.getOperationsServiceBinder().newOperation(service);
+        mWaitingForOpId = mFileActivity.getOperationsServiceBinder().newOperation(service);
         
         mFileActivity.showLoadingDialog();
     }
         
         mFileActivity.showLoadingDialog();
     }
@@ -210,7 +216,7 @@ public class FileOperationsHelper {
         service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
         service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
         service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
         service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
         service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
         service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
-        mFileActivity.getOperationsServiceBinder().newOperation(service);
+        mWaitingForOpId = mFileActivity.getOperationsServiceBinder().newOperation(service);
         
         mFileActivity.showLoadingDialog();
     }
         
         mFileActivity.showLoadingDialog();
     }
@@ -223,7 +229,7 @@ public class FileOperationsHelper {
         service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
         service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
         service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
         service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
         service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
         service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
-        mFileActivity.getOperationsServiceBinder().newOperation(service);
+        mWaitingForOpId =  mFileActivity.getOperationsServiceBinder().newOperation(service);
         
         mFileActivity.showLoadingDialog();
     }
         
         mFileActivity.showLoadingDialog();
     }
@@ -236,8 +242,33 @@ public class FileOperationsHelper {
         service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
         service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
         service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
         service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
         service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
         service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
-        mFileActivity.getOperationsServiceBinder().newOperation(service);
+        mWaitingForOpId =  mFileActivity.getOperationsServiceBinder().newOperation(service);
         
         mFileActivity.showLoadingDialog();
     }
         
         mFileActivity.showLoadingDialog();
     }
+
+    
+    public void cancelTransference(OCFile file) {
+        Account account = mFileActivity.getAccount();
+        FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
+        FileUploaderBinder uploaderBinder =  mFileActivity.getFileUploaderBinder();
+        if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
+            downloaderBinder.cancel(account, file);
+
+        } else if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
+            uploaderBinder.cancel(account, file);
+        }
+    }    
+
+
+    public long getOpIdWaitingFor() {
+        return mWaitingForOpId;
+    }
+
+
+    public void setOpIdWaitingFor(long waitingForOpId) {
+        mWaitingForOpId = waitingForOpId;
+    }
+    
+    
 }
 }