Эх сурвалжийг харах

Get rid of dependency on Context in CreateShareOperation

David A. Velasco 9 жил өмнө
parent
commit
33a9d1f447

+ 2 - 2
res/values/strings.xml

@@ -348,8 +348,8 @@
 	<string name="shared_subject_header">shared</string>
 	<string name="with_you_subject_header">with you</string>
     
-	<string name="subject_token">%1$s shared \"%2$s\" with you</string>
-    <string name="saml_subject_token">\"%1$s\" has been shared with you</string>
+	<string name="subject_user_shared_with_you">%1$s shared \"%2$s\" with you</string>
+    <string name="subject_shared_with_you">\"%1$s\" has been shared with you</string>
 
     <string name="auth_refresh_button">Refresh connection</string>
     <string name="auth_host_address">Server address</string>

+ 37 - 28
src/com/owncloud/android/operations/CreateShareOperation.java

@@ -24,6 +24,7 @@ package com.owncloud.android.operations;
  * Creates a new share from a given file
  */
 
+
 import android.content.Context;
 import android.content.Intent;
 
@@ -47,7 +48,6 @@ public class CreateShareOperation extends SyncOperation {
 
     protected FileDataStorageManager mStorageManager;
 
-    private Context mContext;
     private String mPath;
     private ShareType mShareType;
     private String mShareWith;
@@ -55,10 +55,10 @@ public class CreateShareOperation extends SyncOperation {
     private String mPassword;
     private int mPermissions;
     private Intent mSendIntent;
+    private String mFileName;
 
     /**
      * Constructor
-     * @param context       The context that the share is coming from.
      * @param path          Full path of the file/folder being shared. Mandatory argument
      * @param shareType     0 = user, 1 = group, 3 = Public link. Mandatory argument
      * @param shareWith     User/group ID with who the file should be shared.
@@ -77,34 +77,34 @@ public class CreateShareOperation extends SyncOperation {
      *                      For user or group shares.
      *                      To obtain combinations, add the desired values together.  
      *                      For instance, for Re-Share, delete, read, update, add 16+8+2+1 = 27.
+     *  @param sendIntent   Optional Intent with the information of an app where the link to the new share (if public)
+     *                      should be posted later.
      */
-    public CreateShareOperation(Context context, String path, ShareType shareType, String shareWith,
-                                boolean publicUpload, String password, int permissions,
-                                Intent sendIntent) {
+    public CreateShareOperation(String path, ShareType shareType, String shareWith,
+                                boolean publicUpload, String password, int permissions, Intent sendIntent) {
 
-        mContext = context;
         mPath = path;
         mShareType = shareType;
-        mShareWith = shareWith;
+        mShareWith = shareWith != null ? shareWith : "";
         mPublicUpload = publicUpload;
         mPassword = password;
         mPermissions = permissions;
         mSendIntent = sendIntent;
+        mFileName = null;
     }
 
     @Override
     protected RemoteOperationResult run(OwnCloudClient client) {
-        RemoteOperation operation = null;
-        
         // Check if the share link already exists
-        operation = new GetRemoteSharesForFileOperation(mPath, false, false);
-        RemoteOperationResult result =
-                ((GetRemoteSharesForFileOperation)operation).execute(client);
+        RemoteOperation operation = new GetRemoteSharesForFileOperation(mPath, false, false);
+        RemoteOperationResult result = operation.execute(client);
 
         if (!result.isSuccess() || result.getData().size() <= 0) {
-            operation = new CreateRemoteShareOperation(mPath, mShareType, mShareWith, mPublicUpload,
-                    mPassword, mPermissions);
-            result = ((CreateRemoteShareOperation)operation).execute(client);
+            operation = new CreateRemoteShareOperation(
+                    mPath, mShareType, mShareWith,
+                    mPublicUpload, mPassword, mPermissions
+            );
+            result = operation.execute(client);
         }
         
         if (result.isSuccess()) {
@@ -144,7 +144,28 @@ public class CreateShareOperation extends SyncOperation {
     public Intent getSendIntent() {
         return mSendIntent;
     }
-    
+
+    public Intent getSendIntentWithSubject(Context context) {
+        if (context != null && mSendIntent != null && mSendIntent.getStringExtra(Intent.EXTRA_SUBJECT) != null) {
+            if (getClient() == null || getClient().getCredentials().getUsername() == null) {
+                mSendIntent.putExtra(
+                        Intent.EXTRA_SUBJECT,
+                        context.getString(R.string.subject_shared_with_you, mFileName)
+                );
+            } else {
+                mSendIntent.putExtra(
+                        Intent.EXTRA_SUBJECT,
+                        context.getString(
+                                R.string.subject_user_shared_with_you,
+                                getClient().getCredentials().getUsername(),
+                                mFileName
+                        )
+                );
+            }
+        }
+        return mSendIntent;
+    }
+
     private void updateData(OCShare share) {
         // Update DB with the response
         share.setPath(mPath);
@@ -161,21 +182,9 @@ public class CreateShareOperation extends SyncOperation {
         OCFile file = getStorageManager().getFileByPath(mPath);
         if (file!=null) {
             mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
-            if (getClient().getCredentials().getUsername() == null) {
-                //in saml is null
-                mSendIntent.putExtra(Intent.EXTRA_SUBJECT,
-                        String.format(mContext.getString(R.string.saml_subject_token),
-                                file.getFileName()));
-            } else {
-                mSendIntent.putExtra(Intent.EXTRA_SUBJECT,
-                        String.format(mContext.getString(R.string.subject_token),
-                                getClient().getCredentials().getUsername(), file.getFileName()));
-            }
             file.setPublicLink(share.getShareLink());
             file.setShareViaLink(true);
             getStorageManager().saveFile(file);
-            Log_OC.d(TAG, "Public Link = " + file.getPublicLink());
-
         }
     }
 

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

@@ -555,9 +555,14 @@ public class OperationsService extends Service {
                     String password = operationIntent.getStringExtra(EXTRA_PASSWORD_SHARE);
                     Intent sendIntent = operationIntent.getParcelableExtra(EXTRA_SEND_INTENT);
                     if (remotePath.length() > 0) {
-                        operation = new CreateShareOperation(OperationsService.this, remotePath,
+                        operation = new CreateShareOperation(
+                                remotePath,
                                 ShareType.PUBLIC_LINK,
-                                "", false, password, 1, sendIntent);
+                                null,
+                                false,
+                                password,
+                                1,
+                                sendIntent);
                     }
 
                 } else if (action.equals(ACTION_UNSHARE)) {  // Unshare file

+ 1 - 1
src/com/owncloud/android/ui/activity/FileActivity.java

@@ -762,7 +762,7 @@ public class FileActivity extends AppCompatActivity
             mTryShareAgain = false;
             updateFileFromDB();
 
-            Intent sendIntent = operation.getSendIntent();
+            Intent sendIntent = operation.getSendIntentWithSubject(this);
             startActivity(sendIntent);
         } else {
             // Detect Failure (403) --> needs Password