Browse Source

Merge pull request #398 from owncloud/share_link__unshare_file__share_in_operation_service

Share link  unshare file  share in operation service
David A. Velasco 11 years ago
parent
commit
b015886bd2

+ 14 - 16
src/com/owncloud/android/files/FileOperationsHelper.java

@@ -30,9 +30,7 @@ import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.lib.accounts.OwnCloudAccount;
 import com.owncloud.android.lib.network.webdav.WebdavUtils;
-import com.owncloud.android.lib.operations.common.ShareType;
-import com.owncloud.android.operations.CreateShareOperation;
-import com.owncloud.android.operations.UnshareLinkOperation;
+import com.owncloud.android.services.OperationsService;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.dialog.ActivityChooserDialog;
 import com.owncloud.android.utils.Log_OC;
@@ -109,12 +107,13 @@ public class FileOperationsHelper {
         
         if (file != null) {
             callerActivity.showLoadingDialog();
-            CreateShareOperation createShare = new CreateShareOperation(file.getRemotePath(), ShareType.PUBLIC_LINK, "", false, "", 1, sendIntent);
-            createShare.execute(callerActivity.getStorageManager(), 
-                                callerActivity, 
-                                callerActivity.getRemoteOperationListener(), 
-                                callerActivity.getHandler(), 
-                                callerActivity);
+            
+            Intent service = new Intent(callerActivity, OperationsService.class);
+            service.setAction(OperationsService.ACTION_CREATE_SHARE);
+            service.putExtra(OperationsService.EXTRA_ACCOUNT, callerActivity.getAccount());
+            service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
+            service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
+            callerActivity.startService(service);
             
         } else {
             Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
@@ -146,13 +145,12 @@ public class FileOperationsHelper {
         
         if (isSharedSupported(callerActivity)) {
             // Unshare the file
-            UnshareLinkOperation unshare = new UnshareLinkOperation(file, callerActivity);
-            unshare.execute(callerActivity.getStorageManager(), 
-                    callerActivity, 
-                    callerActivity.getRemoteOperationListener(), 
-                    callerActivity.getHandler(), 
-                    callerActivity);
-         
+            Intent service = new Intent(callerActivity, OperationsService.class);
+            service.setAction(OperationsService.ACTION_UNSHARE);
+            service.putExtra(OperationsService.EXTRA_ACCOUNT, callerActivity.getAccount());
+            service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
+            callerActivity.startService(service);
+            
             callerActivity.showLoadingDialog();
             
         } else {

+ 13 - 11
src/com/owncloud/android/operations/UnshareLinkOperation.java

@@ -26,6 +26,7 @@ import com.owncloud.android.lib.operations.common.RemoteOperationResult;
 import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode;
 import com.owncloud.android.lib.operations.remote.ExistenceCheckRemoteOperation;
 import com.owncloud.android.lib.operations.remote.RemoveRemoteShareOperation;
+import com.owncloud.android.lib.utils.FileUtils;
 import com.owncloud.android.operations.common.SyncOperation;
 import com.owncloud.android.utils.Log_OC;
 
@@ -39,12 +40,12 @@ public class UnshareLinkOperation extends SyncOperation {
 
     private static final String TAG = UnshareLinkOperation.class.getSimpleName();
     
-    private OCFile mFile;
+    private String mRemotePath;
     private Context mContext;
     
     
-    public UnshareLinkOperation(OCFile file, Context context) {
-        mFile = file;
+    public UnshareLinkOperation(String remotePath, Context context) {
+        mRemotePath = remotePath;
         mContext = context;
     }
 
@@ -53,9 +54,9 @@ public class UnshareLinkOperation extends SyncOperation {
         RemoteOperationResult result  = null;
         
         // Get Share for a file
-        String path = mFile.getRemotePath();
-        if (mFile.isFolder()) {
-            path = path.substring(0, path.length()-1); // Remove last /
+        String path = mRemotePath;
+        if (path.endsWith(FileUtils.PATH_SEPARATOR)) {
+           path = path.substring(0, path.length()-1); // Remove last /
         }
         OCShare share = getStorageManager().getShareByPath(path);
         
@@ -66,16 +67,17 @@ public class UnshareLinkOperation extends SyncOperation {
             if (result.isSuccess() || result.getCode() == ResultCode.SHARE_NOT_FOUND) {
                 Log_OC.d(TAG, "Share id = " + share.getIdRemoteShared() + " deleted");
 
-                mFile.setShareByLink(false);
-                mFile.setPublicLink("");
-                getStorageManager().saveFile(mFile);
+                OCFile file = getStorageManager().getFileByPath(mRemotePath);
+                file.setShareByLink(false);
+                file.setPublicLink("");
+                getStorageManager().saveFile(file);
                 getStorageManager().removeShare(share);
                 
                 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
-                    if (existsFile(client, mFile.getRemotePath())) {
+                    if (existsFile(client, file.getRemotePath())) {
                         result = new RemoteOperationResult(ResultCode.OK);
                     } else {
-                        getStorageManager().removeFile(mFile, true, true);
+                        getStorageManager().removeFile(file, true, true);
                     }
                 }
             } 

+ 29 - 2
src/com/owncloud/android/services/OperationsService.java

@@ -28,11 +28,14 @@ import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.lib.network.OnDatatransferProgressListener;
 import com.owncloud.android.lib.network.OwnCloudClientFactory;
 import com.owncloud.android.lib.network.OwnCloudClient;
+import com.owncloud.android.operations.CreateShareOperation;
 import com.owncloud.android.operations.GetSharesOperation;
+import com.owncloud.android.operations.UnshareLinkOperation;
 import com.owncloud.android.operations.common.SyncOperation;
 import com.owncloud.android.lib.operations.common.OnRemoteOperationListener;
 import com.owncloud.android.lib.operations.common.RemoteOperation;
 import com.owncloud.android.lib.operations.common.RemoteOperationResult;
+import com.owncloud.android.lib.operations.common.ShareType;
 import com.owncloud.android.utils.Log_OC;
 
 import android.accounts.Account;
@@ -56,7 +59,12 @@ public class OperationsService extends Service {
     
     public static final String EXTRA_ACCOUNT = "ACCOUNT";
     public static final String EXTRA_SERVER_URL = "SERVER_URL";
-    public static final String EXTRA_RESULT = "RESULT";    
+    public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
+    public static final String EXTRA_SEND_INTENT = "SEND_INTENT";
+    public static final String EXTRA_RESULT = "RESULT";
+    
+    public static final String ACTION_CREATE_SHARE = "CREATE_SHARE";
+    public static final String ACTION_UNSHARE = "UNSHARE";
     
     public static final String ACTION_OPERATION_ADDED = OperationsService.class.getName() + ".OPERATION_ADDED";
     public static final String ACTION_OPERATION_FINISHED = OperationsService.class.getName() + ".OPERATION_FINISHED";
@@ -112,8 +120,27 @@ public class OperationsService extends Service {
         try {
             Account account = intent.getParcelableExtra(EXTRA_ACCOUNT);
             String serverUrl = intent.getStringExtra(EXTRA_SERVER_URL);
+            
             Target target = new Target(account, (serverUrl == null) ? null : Uri.parse(serverUrl));
-            GetSharesOperation operation = new GetSharesOperation();
+            RemoteOperation operation = null;
+            
+            String action = intent.getAction();
+            if (action == ACTION_CREATE_SHARE) {  // Create Share
+                String remotePath = intent.getStringExtra(EXTRA_REMOTE_PATH);
+                Intent sendIntent = intent.getParcelableExtra(EXTRA_SEND_INTENT);
+                if (remotePath.length() > 0) {
+                    operation = new CreateShareOperation(remotePath, ShareType.PUBLIC_LINK, 
+                            "", false, "", 1, sendIntent);
+                }
+            } else if (action == ACTION_UNSHARE) {  // Unshare file
+                String remotePath = intent.getStringExtra(EXTRA_REMOTE_PATH);
+                if (remotePath.length() > 0) {
+                    operation = new UnshareLinkOperation(remotePath, this.getApplicationContext());
+                }
+            } else {
+                operation = new GetSharesOperation();
+            }
+            
             mPendingOperations.add(new Pair<Target , RemoteOperation>(target, operation));
             sendBroadcastNewOperation(target, operation);