From 45770845174951ccbd92a421a03fc87afa99d14d Mon Sep 17 00:00:00 2001 From: Tilo Spannagel Date: Wed, 29 Aug 2018 15:52:08 +0200 Subject: [PATCH] Improve share link handling Signed-off-by: Tilo Spannagel --- .../ui/activity/FileDisplayActivity.java | 91 +++++++++++-------- .../owncloud/android/utils/ClipboardUtil.java | 8 +- 2 files changed, 58 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java index 238a55c15a..89ff01e4ce 100644 --- a/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/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; @@ -1907,9 +1908,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 = ""; @@ -1920,44 +1918,14 @@ public class FileDisplayActivity extends HookActivity break; } } + String fLink = link; - 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); + ClipboardUtil.copyToClipboard(this, link, false); + Snackbar.make( + findViewById(android.R.id.content), + R.string.clipboard_text_copied, + Snackbar.LENGTH_LONG + ).setAction(R.string.share, v -> showShareLinkDialog(fLink)).show(); if (fileDetailFragment != null && fileDetailFragment.getFileDetailSharingFragment() != null) { fileDetailFragment.getFileDetailSharingFragment().refreshPublicShareFromDB(); @@ -1992,6 +1960,49 @@ public class FileDisplayActivity extends HookActivity } } + private 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(); diff --git a/src/main/java/com/owncloud/android/utils/ClipboardUtil.java b/src/main/java/com/owncloud/android/utils/ClipboardUtil.java index 632aea487d..395fc7c073 100644 --- a/src/main/java/com/owncloud/android/utils/ClipboardUtil.java +++ b/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);