Browse Source

Merge pull request #2935 from nextcloud/improve-share-link-handling

Improve share link handling
Andy Scherzinger 6 years ago
parent
commit
5ed69587e2

+ 9 - 0
CHANGELOG.md

@@ -1,3 +1,12 @@
+## 3.2.4 (Sept, 04, 2018)
+fix push notification on gplay release
+
+## 3.2.3 (Aug, 21, 2018)
+- Fix crash on Android Android 4.x
+
+## 3.2.2 (Aug, 20, 2018)
+- New simple signup screen
+
 ## 3.2.1 (June, 11, 2018)
 - Enhanced file detail/sharing screen for mail-shares
 - Fix local sorting and file selection

+ 63 - 45
src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -123,6 +123,7 @@ import com.owncloud.android.ui.preview.PreviewImageFragment;
 import com.owncloud.android.ui.preview.PreviewMediaFragment;
 import com.owncloud.android.ui.preview.PreviewTextFragment;
 import com.owncloud.android.ui.preview.PreviewVideoActivity;
+import com.owncloud.android.utils.ClipboardUtil;
 import com.owncloud.android.utils.DataHolderUtil;
 import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.ErrorMessageAdapter;
@@ -276,9 +277,7 @@ public class FileDisplayActivity extends HookActivity
                                 PermissionUtil.requestWriteExternalStoreagePermission(FileDisplayActivity.this);
                             }
                         });
-
                 ThemeUtils.colorSnackbar(this, snackbar);
-
                 snackbar.show();
             } else {
                 // No explanation needed, request the permission.
@@ -1907,9 +1906,6 @@ public class FileDisplayActivity extends HookActivity
         if (result.isSuccess()) {
             updateFileFromDB();
 
-            // Create dialog to allow the user choose an app to send the link
-            Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
-
             // if share to user and share via link multiple ocshares are returned,
             // therefore filtering for public_link
             String link = "";
@@ -1921,43 +1917,7 @@ public class FileDisplayActivity extends HookActivity
                 }
             }
 
-            intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
-            intentToShareLink.setType("text/plain");
-
-            String username;
-            try {
-                OwnCloudAccount oca = new OwnCloudAccount(getAccount(), this);
-                if (oca.getDisplayName() != null && !oca.getDisplayName().isEmpty()) {
-                    username = oca.getDisplayName();
-                } else {
-                    username = AccountUtils.getUsernameForAccount(getAccount());
-                }
-            } catch (Exception e) {
-                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);
+            copyAndShareFileLink(link);
 
             if (fileDetailFragment != null && fileDetailFragment.getFileDetailSharingFragment() != null) {
                 fileDetailFragment.getFileDetailSharingFragment().refreshPublicShareFromDB();
@@ -1983,15 +1943,71 @@ public class FileDisplayActivity extends HookActivity
                 if (fileDetailFragment != null && fileDetailFragment.getFileDetailSharingFragment() != null) {
                     fileDetailFragment.getFileDetailSharingFragment().refreshPublicShareFromDB();
                 }
-                Snackbar.make(
+                Snackbar snackbar = Snackbar.make(
                         findViewById(android.R.id.content),
                         ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
                         Snackbar.LENGTH_LONG
-                ).show();
+                );
+                ThemeUtils.colorSnackbar(this, snackbar);
+                snackbar.show();
             }
         }
     }
 
+    private void copyAndShareFileLink(String link) {
+        ClipboardUtil.copyToClipboard(this, link, false);
+        Snackbar snackbar = Snackbar.make(
+                findViewById(android.R.id.content),
+                R.string.clipboard_text_copied,
+                Snackbar.LENGTH_LONG
+        ).setAction(R.string.share, v -> showShareLinkDialog(link));
+        ThemeUtils.colorSnackbar(this, snackbar);
+        snackbar.show();
+    }
+
+    public void showShareLinkDialog(String link) {
+        // Create dialog to allow the user choose an app to send the link
+        Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
+
+        intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
+        intentToShareLink.setType("text/plain");
+
+        String username;
+        try {
+            OwnCloudAccount oca = new OwnCloudAccount(getAccount(), this);
+            if (oca.getDisplayName() != null && !oca.getDisplayName().isEmpty()) {
+                username = oca.getDisplayName();
+            } else {
+                username = AccountUtils.getUsernameForAccount(getAccount());
+            }
+        } catch (Exception e) {
+            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);
+    }
+
     private void onUpdateShareInformation(RemoteOperationResult result, @StringRes int errorString) {
         Fragment fileDetailFragment = getSecondFragment();
 
@@ -1999,7 +2015,9 @@ public class FileDisplayActivity extends HookActivity
             updateFileFromDB();
             refreshListOfFilesFragment(false);
         } else if (fileDetailFragment.getView() != null) {
-            Snackbar.make(fileDetailFragment.getView(), errorString, Snackbar.LENGTH_LONG).show();
+            Snackbar snackbar = Snackbar.make(fileDetailFragment.getView(), errorString, Snackbar.LENGTH_LONG);
+            ThemeUtils.colorSnackbar(this, snackbar);
+            snackbar.show();
         }
 
         if (fileDetailFragment != null && fileDetailFragment instanceof FileDetailFragment) {

+ 24 - 12
src/main/java/com/owncloud/android/ui/fragment/FileDetailSharingFragment.java

@@ -54,6 +54,7 @@ import com.owncloud.android.lib.resources.shares.SharePermissionsBuilder;
 import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.lib.resources.status.OCCapability;
 import com.owncloud.android.ui.activity.FileActivity;
+import com.owncloud.android.ui.activity.FileDisplayActivity;
 import com.owncloud.android.ui.adapter.UserListAdapter;
 import com.owncloud.android.ui.decoration.SimpleListItemDividerDecoration;
 import com.owncloud.android.ui.dialog.ExpirationDatePickerDialogFragment;
@@ -235,24 +236,31 @@ public class FileDetailSharingFragment extends Fragment implements UserListAdapt
     @OnClick(R.id.share_by_link)
     public void toggleShareByLink() {
         if (shareByLink.isChecked()) {
-            if (capabilities != null &&
-                    capabilities.getFilesSharingPublicPasswordEnforced().isTrue()) {
-                // password enforced by server, request to the user before trying to create
-                requestPasswordForShareViaLink(true);
+            createShareLink();
+        } else {
+            ((FileActivity) getActivity()).getFileOperationsHelper().unshareFileViaLink(file);
+        }
+    }
 
-            } else {
-                // create without password if not enforced by server or we don't know if enforced;
-                ((FileActivity) getActivity()).getFileOperationsHelper().shareFileViaLink(file, null);
-            }
+    private void createShareLink() {
+        if (capabilities != null &&
+                capabilities.getFilesSharingPublicPasswordEnforced().isTrue()) {
+            // password enforced by server, request to the user before trying to create
+            requestPasswordForShareViaLink(true);
 
         } else {
-            ((FileActivity) getActivity()).getFileOperationsHelper().unshareFileViaLink(file);
+            // create without password if not enforced by server or we don't know if enforced;
+            ((FileActivity) getActivity()).getFileOperationsHelper().shareFileViaLink(file, null);
         }
     }
 
     private void showSendLinkTo() {
         if (file.isSharedViaLink()) {
-            ((FileActivity) getActivity()).getFileOperationsHelper().getFileWithLink(file);
+            if (TextUtils.isEmpty(file.getPublicLink())) {
+                ((FileActivity) getActivity()).getFileOperationsHelper().getFileWithLink(file);
+            } else {
+                ((FileDisplayActivity) getActivity()).showShareLinkDialog(file.getPublicLink());
+            }
         }
     }
 
@@ -260,7 +268,7 @@ public class FileDetailSharingFragment extends Fragment implements UserListAdapt
     public void copyLinkToClipboard() {
         if (file.isSharedViaLink()) {
             if (TextUtils.isEmpty(file.getPublicLink())) {
-                showSendLinkTo();
+                ((FileActivity) getActivity()).getFileOperationsHelper().getFileWithLink(file);
             } else {
                 ClipboardUtil.copyToClipboard(getActivity(), file.getPublicLink());
             }
@@ -336,7 +344,11 @@ public class FileDetailSharingFragment extends Fragment implements UserListAdapt
                 return true;
             }
             case R.id.action_share_send_link: {
-                showSendLinkTo();
+                if(shareByLink.isChecked() && file.isSharedViaLink() && !TextUtils.isEmpty(file.getPublicLink())) {
+                    ((FileDisplayActivity) getActivity()).showShareLinkDialog(file.getPublicLink());
+                } else {
+                    showSendLinkTo();
+                }
                 return true;
             }
             default:

+ 7 - 1
src/main/java/com/owncloud/android/utils/ClipboardUtil.java

@@ -39,6 +39,10 @@ public class ClipboardUtil {
     }
 
     public static void copyToClipboard(Activity activity, String text) {
+        copyToClipboard(activity, text, true);
+    }
+
+    public static void copyToClipboard(Activity activity, String text, boolean showToast) {
         if (text != null && text.length() > 0) {
             try {
                 ClipData clip = ClipData.newPlainText(
@@ -48,7 +52,9 @@ public class ClipboardUtil {
                 );
                 ((ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE)).setPrimaryClip(clip);
 
-                Toast.makeText(activity, R.string.clipboard_text_copied, Toast.LENGTH_SHORT).show();
+                if (showToast) {
+                    Toast.makeText(activity, R.string.clipboard_text_copied, Toast.LENGTH_SHORT).show();
+                }
             } catch (Exception e) {
                 Toast.makeText(activity, R.string.clipboard_unexpected_error, Toast.LENGTH_SHORT).show();
                 Log_OC.e(TAG, "Exception caught while copying to clipboard", e);