Browse Source

Error correctly reported when share password could not be set, and clean-up of unused code for sharing via link

David A. Velasco 9 years ago
parent
commit
154f6d03e0

+ 1 - 1
owncloud-android-library

@@ -1 +1 @@
-Subproject commit 39e3ddaa07b0943b034b34a84a33b4dc4c7475d0
+Subproject commit 19e30a6236ca75a6e223ba6b7b0bd901b6615e12

+ 2 - 1
src/com/owncloud/android/authentication/AccountUtils.java

@@ -194,7 +194,8 @@ public class AccountUtils {
                 for (Account account : ocAccounts) {
                     // build new account name
                     serverUrl = accountMgr.getUserData(account, Constants.KEY_OC_BASE_URL);
-                    username = account.name.substring(0, account.name.lastIndexOf('@'));
+                    username = com.owncloud.android.lib.common.accounts.AccountUtils.
+                            getUsernameForAccount(account);
                     newAccountName = com.owncloud.android.lib.common.accounts.AccountUtils.
                             buildAccountName(Uri.parse(serverUrl), username);
 

+ 3 - 1
src/com/owncloud/android/authentication/AuthenticatorActivity.java

@@ -494,7 +494,9 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity
         boolean isPasswordExposed = false;
         if (savedInstanceState == null) {
             if (mAccount != null) {
-                presetUserName = mAccount.name.substring(0, mAccount.name.lastIndexOf('@'));
+                presetUserName =
+                    com.owncloud.android.lib.common.accounts.AccountUtils.
+                        getUsernameForAccount(mAccount);
             }
             
         } else {

+ 0 - 36
src/com/owncloud/android/files/FileOperationsHelper.java

@@ -205,25 +205,6 @@ public class FileOperationsHelper {
         }
     }
 
-    public void shareFileWithLinkToApp(OCFile file, String password, Intent sendIntent) {
-        
-        if (file != null) {
-            mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
-                    getString(R.string.wait_a_moment));
-
-            Intent service = new Intent(mFileActivity, OperationsService.class);
-            service.setAction(OperationsService.ACTION_CREATE_SHARE_VIA_LINK);
-            service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
-            service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
-            service.putExtra(OperationsService.EXTRA_SHARE_PASSWORD, password);
-            service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
-            mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
-            
-        } else {
-            Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
-        }
-    }
-
     /**
      * Helper method to share a file with a known sharee. Starts a request to do it in {@link OperationsService}
      *
@@ -331,23 +312,6 @@ public class FileOperationsHelper {
     }
 
 
-    /**
-     * Starts a dialog that requests a password to the user to protect a share link.
-     *
-     * @param   file            File which public share will be protected by the requested password
-     * @param   createShare     When 'true', the request for password will be followed by the creation of a new
-     *                          public link; when 'false', a public share is assumed to exist, and the password
-     *                          is bound to it.
-     */
-    public void requestPasswordForShareViaLink(OCFile file, boolean createShare) {
-        SharePasswordDialogFragment dialog =
-                SharePasswordDialogFragment.newInstance(file, createShare);
-        dialog.show(
-            mFileActivity.getSupportFragmentManager(),
-            SharePasswordDialogFragment.PASSWORD_FRAGMENT
-        );
-    }
-
     /**
      * Updates a public share on a file to set its password.
      * Starts a request to do it in {@link OperationsService}

+ 1 - 41
src/com/owncloud/android/operations/CreateShareViaLinkOperation.java

@@ -26,10 +26,6 @@ package com.owncloud.android.operations;
  */
 
 
-import android.content.Context;
-import android.content.Intent;
-
-import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
@@ -47,27 +43,20 @@ public class CreateShareViaLinkOperation extends SyncOperation {
 
     private String mPath;
     private String mPassword;
-    private Intent mSendIntent;
-    private String mFileName;
 
     /**
      * Constructor
      * @param path          Full path of the file/folder being shared. Mandatory argument
      * @param password      Password to protect a public link share.
      *                      Only available for public link shares
-     *  @param sendIntent   Optional Intent with the information of an app where the link to the new share (if public)
-     *                      should be posted later.
      */
     public CreateShareViaLinkOperation(
             String path,
-            String password,
-            Intent sendIntent
+            String password
     ) {
 
         mPath = path;
         mPassword = password;
-        mSendIntent = sendIntent;
-        mFileName = null;
     }
 
     @Override
@@ -129,32 +118,6 @@ public class CreateShareViaLinkOperation extends SyncOperation {
         return mPassword;
     }
 
-    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() == 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);
@@ -172,9 +135,6 @@ public class CreateShareViaLinkOperation extends SyncOperation {
             file.setPublicLink(share.getShareLink());
             file.setShareViaLink(true);
             getStorageManager().saveFile(file);
-            if (mSendIntent != null) {
-                mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
-            }
         }
     }
 

+ 1 - 1
src/com/owncloud/android/providers/FileContentProvider.java

@@ -897,7 +897,7 @@ public class FileContentProvider extends ContentProvider {
 			for (Account account : accounts) {
                 // build both old and new account name
                 serverUrl = ama.getUserData(account, AccountUtils.Constants.KEY_OC_BASE_URL);
-                username = account.name.substring(0, account.name.lastIndexOf('@'));
+                username = AccountUtils.getUsernameForAccount(account);
                 oldAccountName = AccountUtils.buildAccountNameOld(Uri.parse(serverUrl), username);
                 newAccountName = AccountUtils.buildAccountName(Uri.parse(serverUrl), username);
 

+ 1 - 4
src/com/owncloud/android/services/OperationsService.java

@@ -83,7 +83,6 @@ public class OperationsService extends Service {
     public static final String EXTRA_SERVER_URL = "SERVER_URL";
     public static final String EXTRA_OAUTH2_QUERY_PARAMETERS = "OAUTH2_QUERY_PARAMETERS";
     public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
-    public static final String EXTRA_SEND_INTENT = "SEND_INTENT";
     public static final String EXTRA_NEWNAME = "NEWNAME";
     public static final String EXTRA_REMOVE_ONLY_LOCAL = "REMOVE_LOCAL_COPY";
     public static final String EXTRA_CREATE_FULL_PATH = "CREATE_FULL_PATH";
@@ -563,12 +562,10 @@ public class OperationsService extends Service {
                 if (action.equals(ACTION_CREATE_SHARE_VIA_LINK)) {  // Create public share via link
                     String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                     String password = operationIntent.getStringExtra(EXTRA_SHARE_PASSWORD);
-                    Intent sendIntent = operationIntent.getParcelableExtra(EXTRA_SEND_INTENT);
                     if (remotePath.length() > 0) {
                         operation = new CreateShareViaLinkOperation(
                                 remotePath,
-                                password,
-                                sendIntent
+                                password
                         );
                     }
 

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

@@ -111,7 +111,6 @@ public class FileActivity extends AppCompatActivity
     private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
 
     private static final String KEY_WAITING_FOR_OP_ID = "WAITING_FOR_OP_ID";
-    private static final String DIALOG_SHARE_PASSWORD = "DIALOG_SHARE_PASSWORD";
     private static final String KEY_ACTION_BAR_TITLE = "ACTION_BAR_TITLE";
 
     public static final int REQUEST_CODE__UPDATE_CREDENTIALS = 0;
@@ -789,9 +788,6 @@ public class FileActivity extends AppCompatActivity
                 t.show();
             }
 
-        } else if (operation instanceof CreateShareViaLinkOperation) {
-            onCreateShareViaLinkOperationFinish((CreateShareViaLinkOperation) operation, result);
-
         } else if (operation instanceof SynchronizeFileOperation) {
             onSynchronizeFileOperationFinish((SynchronizeFileOperation) operation, result);
 
@@ -887,44 +883,6 @@ public class FileActivity extends AppCompatActivity
         }
     }
 
-    private void onCreateShareViaLinkOperationFinish(CreateShareViaLinkOperation operation,
-                                                     RemoteOperationResult result) {
-        if (result.isSuccess()) {
-            updateFileFromDB();
-
-            Intent sendIntent = operation.getSendIntentWithSubject(this);
-            if (sendIntent != null) {
-                startActivity(sendIntent);
-            }
-
-        } else {
-            // Detect Failure (403) --> needs Password
-            if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
-                String password = operation.getPassword();
-                if ((password == null || password.length() == 0) &&
-                    getCapabilities().getFilesSharingPublicEnabled().isUnknown())
-                    {
-                    // Was tried without password, but not sure that it's optional. Try with password.
-                    // Try with password before giving up.
-                    // See also ShareFileFragment#OnShareViaLinkListener
-                    SharePasswordDialogFragment dialog =
-                            SharePasswordDialogFragment.newInstance(new OCFile(operation.getPath()), true);
-                    dialog.show(getSupportFragmentManager(), DIALOG_SHARE_PASSWORD);
-                } else {
-                    Toast t = Toast.makeText(this,
-                        ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
-                        Toast.LENGTH_LONG);
-                    t.show();
-                }
-            } else {
-                Toast t = Toast.makeText(this,
-                        ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
-                        Toast.LENGTH_LONG);
-                t.show();
-            }
-        }
-    }
-
     private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation,
                                                   RemoteOperationResult result) {
         OCFile syncedFile = operation.getLocalFile();

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

@@ -300,11 +300,7 @@ public class Preferences extends PreferenceActivity
                         
                         String appName = getString(R.string.app_name);
                         String downloadUrl = getString(R.string.url_app_download);
-                        Account currentAccount = AccountUtils.
-                                getCurrentOwnCloudAccount(Preferences.this);
-                        String username = currentAccount.name.substring(0,
-                                currentAccount.name.lastIndexOf('@'));
-                        
+
                         String recommendSubject =
                                 String.format(getString(R.string.recommend_subject),
                                 appName);

+ 71 - 14
src/com/owncloud/android/ui/activity/ShareActivity.java

@@ -28,9 +28,11 @@ import android.os.Bundle;
 import android.support.v4.app.DialogFragment;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentTransaction;
+import android.widget.Toast;
 
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.lib.common.accounts.AccountUtils;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
@@ -42,10 +44,12 @@ import com.owncloud.android.operations.UnshareOperation;
 import com.owncloud.android.operations.UpdateSharePermissionsOperation;
 import com.owncloud.android.providers.UsersAndGroupsSearchProvider;
 import com.owncloud.android.ui.dialog.ShareLinkToDialog;
+import com.owncloud.android.ui.dialog.SharePasswordDialogFragment;
 import com.owncloud.android.ui.fragment.EditShareFragment;
 import com.owncloud.android.ui.fragment.SearchShareesFragment;
 import com.owncloud.android.ui.fragment.ShareFileFragment;
 import com.owncloud.android.ui.fragment.ShareFragmentListener;
+import com.owncloud.android.utils.ErrorMessageAdapter;
 import com.owncloud.android.utils.GetShareWithUsersAsyncTask;
 
 
@@ -62,10 +66,10 @@ public class ShareActivity extends FileActivity
     private static final String TAG_SEARCH_FRAGMENT = "SEARCH_USER_AND_GROUPS_FRAGMENT";
     private static final String TAG_EDIT_SHARE_FRAGMENT = "EDIT_SHARE_FRAGMENT";
 
-    /**
-     * Tag for dialog
-     */
+    /// Tags for dialog fragments
     private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
+    private static final String FTAG_SHARE_PASSWORD_DIALOG = "SHARE_PASSWORD_DIALOG";
+
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -207,17 +211,8 @@ public class ShareActivity extends FileActivity
             refreshSharesFromStorageManager();
         }
 
-        if (operation instanceof CreateShareViaLinkOperation && result.isSuccess()) {
-            // Send link to the app
-            String link = ((OCShare) (result.getData().get(0))).getShareLink();
-            Log_OC.d(TAG, "Share link = " + link);
-
-            Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
-            intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
-            intentToShareLink.setType("text/plain");
-            String[] packagesToExclude = new String[]{getPackageName()};
-            DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intentToShareLink, packagesToExclude);
-            chooserDialog.show(getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
+        if (operation instanceof CreateShareViaLinkOperation) {
+            onCreateShareViaLinkOperationFinish((CreateShareViaLinkOperation) operation, result);
         }
 
         if (operation instanceof UnshareOperation && result.isSuccess() && getEditShareFragment() != null) {
@@ -286,4 +281,66 @@ public class ShareActivity extends FileActivity
         return (EditShareFragment) getSupportFragmentManager().findFragmentByTag(TAG_EDIT_SHARE_FRAGMENT);
     }
 
+
+    private void onCreateShareViaLinkOperationFinish(CreateShareViaLinkOperation operation,
+                                                     RemoteOperationResult result) {
+        if (result.isSuccess()) {
+            updateFileFromDB();
+
+            // Create dialog to allow the user choose an app to send the link
+            Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
+            String link = ((OCShare) (result.getData().get(0))).getShareLink();
+            intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
+            intentToShareLink.setType("text/plain");
+            String username = AccountUtils.getUsernameForAccount(getAccount());
+            if (username != null) {
+                intentToShareLink.putExtra(
+                    Intent.EXTRA_SUBJECT,
+                    getString(
+                        R.string.subject_user_shared_with_you,
+                        username,
+                        getFile().getFileName()
+                    )
+                );
+            } else {
+                intentToShareLink.putExtra(
+                    Intent.EXTRA_SUBJECT,
+                    getString(
+                        R.string.subject_shared_with_you,
+                        getFile().getFileName()
+                    )
+                );
+            }
+
+            String[] packagesToExclude = new String[]{getPackageName()};
+            DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intentToShareLink, packagesToExclude);
+            chooserDialog.show(getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
+
+        } else {
+            // Detect Failure (403) --> maybe needs password
+            String password = operation.getPassword();
+            if (result.getCode() == RemoteOperationResult.ResultCode.SHARE_FORBIDDEN    &&
+                    (password == null || password.length() == 0)                        &&
+                    getCapabilities().getFilesSharingPublicEnabled().isUnknown()) {
+                    // Was tried without password, but not sure that it's optional.
+
+                // Try with password before giving up; see also ShareFileFragment#OnShareViaLinkListener
+                ShareFileFragment shareFileFragment = getShareFileFragment();
+                if (shareFileFragment != null
+                    && shareFileFragment.isAdded()) {   // only if added to the view hierarchy!!
+
+                    shareFileFragment.requestPasswordForShareViaLink(true);
+                }
+
+            } else {
+                Toast t = Toast.makeText(this,
+                    ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
+                    Toast.LENGTH_LONG);
+                t.show();
+            }
+        }
+
+    }
+
+
 }

+ 3 - 2
src/com/owncloud/android/ui/dialog/SharePasswordDialogFragment.java

@@ -55,8 +55,9 @@ public class SharePasswordDialogFragment extends DialogFragment
      * Public factory method to create new SharePasswordDialogFragment instances.
      *
      * @param   file            OCFile bound to the public share that which password will be set or updated
-     * @param   createShare     When 'true', the public share will be created; when 'false', will be assumed
-     *                          that the public share already exists, and its state will be directly updated.
+     * @param   createShare     When 'true', the request for password will be followed by the creation of a new
+     *                          public link; when 'false', a public share is assumed to exist, and the password
+     *                          is bound to it.
      * @return                  Dialog ready to show.
      */
     public static SharePasswordDialogFragment newInstance(OCFile file, boolean createShare) {

+ 20 - 8
src/com/owncloud/android/ui/fragment/ShareFileFragment.java

@@ -51,6 +51,7 @@ import com.owncloud.android.lib.resources.status.OCCapability;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.adapter.ShareUserListAdapter;
 import com.owncloud.android.ui.dialog.ExpirationDatePickerDialogFragment;
+import com.owncloud.android.ui.dialog.SharePasswordDialogFragment;
 import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.MimetypeIconUtil;
 
@@ -279,16 +280,16 @@ public class ShareFileFragment extends Fragment
                 if (mCapabilities != null &&
                         mCapabilities.getFilesSharingPublicPasswordEnforced().isTrue()) {
                     // password enforced by server, request to the user before trying to create
-                    ((FileActivity) getActivity()).getFileOperationsHelper().
-                            requestPasswordForShareViaLink(mFile, true);
+                    requestPasswordForShareViaLink(true);
 
                 } else {
                     // create without password if not enforced by server or we don't know if enforced;
                     ((FileActivity) getActivity()).getFileOperationsHelper().
                             shareFileViaLink(mFile, null);
 
-                    // FileActivtiy#onCreateShareViaLinkOperationFinish still handles the guess of enforcement
-                    // for server in versions previous to OwnCloudVersion#MINIMUM_VERSION_CAPABILITIES_API
+                    // ShareActivity#onCreateShareViaLinkOperationFinish will take care if password
+                    // is enforced by the server but app doesn't know, or if server version is
+                    // older than OwnCloudVersion#MINIMUM_VERSION_CAPABILITIES_API
                 }
 
             } else {
@@ -429,8 +430,7 @@ public class ShareFileFragment extends Fragment
                 return;
             }
             if (isChecked) {
-                ((FileActivity) getActivity()).getFileOperationsHelper().
-                        requestPasswordForShareViaLink(mFile, false);
+                requestPasswordForShareViaLink(false);
             } else {
                 ((FileActivity) getActivity()).getFileOperationsHelper().
                         setPasswordToShareViaLink(mFile, "");   // "" clears
@@ -451,8 +451,7 @@ public class ShareFileFragment extends Fragment
         @Override
         public void onClick(View passwordView) {
             if (mPublicShare != null && mPublicShare.isPasswordProtected()) {
-                ((FileActivity) getActivity()).getFileOperationsHelper().
-                        requestPasswordForShareViaLink(mFile, false);
+                requestPasswordForShareViaLink(false);
             }
         }
     }
@@ -844,4 +843,17 @@ public class ShareFileFragment extends Fragment
         listView.requestLayout();
     }
 
+
+    /**
+     * Starts a dialog that requests a password to the user to protect a share link.
+     *
+     * @param   createShare     When 'true', the request for password will be followed by the creation of a new
+     *                          public link; when 'false', a public share is assumed to exist, and the password
+     *                          is bound to it.
+     */
+    public void requestPasswordForShareViaLink(boolean createShare) {
+        SharePasswordDialogFragment dialog = SharePasswordDialogFragment.newInstance(mFile, createShare);
+        dialog.show(getFragmentManager(),SharePasswordDialogFragment.PASSWORD_FRAGMENT);
+    }
+
 }