Ver código fonte

copy link action on image/video preview

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
tobiasKaminsky 6 anos atrás
pai
commit
cc4d340bd5

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

@@ -1,4 +1,4 @@
-/**
+/*
  *   ownCloud Android client application
  *
  *   @author David A. Velasco
@@ -69,13 +69,16 @@ import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
 import com.owncloud.android.ui.asynctasks.LoadingVersionNumberTask;
 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
 import com.owncloud.android.ui.dialog.LoadingDialog;
+import com.owncloud.android.ui.dialog.ShareLinkToDialog;
 import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
 import com.owncloud.android.ui.helpers.FileOperationsHelper;
+import com.owncloud.android.utils.ClipboardUtil;
 import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.ErrorMessageAdapter;
 import com.owncloud.android.utils.FilesSyncHelper;
 import com.owncloud.android.utils.ThemeUtils;
 
+import androidx.fragment.app.DialogFragment;
 import androidx.fragment.app.Fragment;
 import androidx.fragment.app.FragmentManager;
 import androidx.fragment.app.FragmentTransaction;
@@ -654,4 +657,49 @@ public abstract class FileActivity extends DrawerActivity
             DisplayUtils.showSnackMessage(activity, R.string.dev_version_no_new_version_available, Snackbar.LENGTH_LONG);
         }
     }
+
+    public static void copyAndShareFileLink(FileActivity activity, String link) {
+        ClipboardUtil.copyToClipboard(activity, link, false);
+        Snackbar snackbar = Snackbar.make(activity.findViewById(android.R.id.content), R.string.clipboard_text_copied,
+                                          Snackbar.LENGTH_LONG)
+            .setAction(R.string.share, v -> showShareLinkDialog(activity, link));
+        ThemeUtils.colorSnackbar(activity, snackbar);
+        snackbar.show();
+    }
+
+    public static void showShareLinkDialog(FileActivity activity, 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(activity.getAccount(), activity);
+            if (oca.getDisplayName() != null && !oca.getDisplayName().isEmpty()) {
+                username = oca.getDisplayName();
+            } else {
+                username = com.owncloud.android.lib.common.accounts.AccountUtils
+                    .getUsernameForAccount(activity.getAccount());
+            }
+        } catch (Exception e) {
+            username = com.owncloud.android.lib.common.accounts.AccountUtils
+                .getUsernameForAccount(activity.getAccount());
+        }
+
+        if (username != null) {
+            intentToShareLink.putExtra(Intent.EXTRA_SUBJECT,
+                                       activity.getString(R.string.subject_user_shared_with_you, username,
+                                                          activity.getFile().getFileName()));
+        } else {
+            intentToShareLink.putExtra(Intent.EXTRA_SUBJECT,
+                                       activity.getString(R.string.subject_shared_with_you,
+                                                          activity.getFile().getFileName()));
+        }
+
+        String[] packagesToExclude = new String[]{activity.getPackageName()};
+        DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intentToShareLink, packagesToExclude);
+        chooserDialog.show(activity.getSupportFragmentManager(), FileDisplayActivity.FTAG_CHOOSER_DIALOG);
+    }
 }

+ 10 - 62
src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -68,7 +68,6 @@ import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
-import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.accounts.AccountUtils;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
@@ -98,7 +97,6 @@ import com.owncloud.android.providers.UsersAndGroupsSearchProvider;
 import com.owncloud.android.syncadapter.FileSyncAdapter;
 import com.owncloud.android.ui.asynctasks.CheckAvailableSpaceTask;
 import com.owncloud.android.ui.dialog.SendShareDialog;
-import com.owncloud.android.ui.dialog.ShareLinkToDialog;
 import com.owncloud.android.ui.dialog.SortingOrderDialogFragment;
 import com.owncloud.android.ui.events.SyncEventFinished;
 import com.owncloud.android.ui.events.TokenPushEvent;
@@ -115,7 +113,6 @@ 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;
@@ -141,7 +138,6 @@ import androidx.appcompat.app.AlertDialog;
 import androidx.appcompat.widget.SearchView;
 import androidx.core.content.ContextCompat;
 import androidx.core.view.MenuItemCompat;
-import androidx.fragment.app.DialogFragment;
 import androidx.fragment.app.Fragment;
 import androidx.fragment.app.FragmentManager;
 import androidx.fragment.app.FragmentTransaction;
@@ -166,8 +162,8 @@ public class FileDisplayActivity extends HookActivity
     private View mLeftFragmentContainer;
     private View mRightFragmentContainer;
 
-    private static final String TAG_PUBLIC_LINK = "PUBLIC_LINK";
-    private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
+    public static final String TAG_PUBLIC_LINK = "PUBLIC_LINK";
+    public static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
 
     private static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
     private static final String KEY_SYNC_IN_PROGRESS = "SYNC_IN_PROGRESS";
@@ -1975,7 +1971,7 @@ public class FileDisplayActivity extends HookActivity
                 }
             }
 
-            copyAndShareFileLink(link);
+            copyAndShareFileLink(this, link);
 
             if (fileDetailFragment != null && fileDetailFragment.getFileDetailSharingFragment() != null) {
                 fileDetailFragment.getFileDetailSharingFragment().refreshPublicShareFromDB();
@@ -2025,60 +2021,6 @@ public class FileDisplayActivity extends HookActivity
         }
     }
 
-    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 defaultError) {
         Snackbar snackbar;
         Fragment fileDetailFragment = getSecondFragment();
@@ -2117,7 +2059,13 @@ public class FileDisplayActivity extends HookActivity
      * @return A {@link FileDetailFragment} instance, or null
      */
     private FileDetailFragment getShareFileFragment() {
-        return (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(TAG_SECOND_FRAGMENT);
+        Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_SECOND_FRAGMENT);
+
+        if (fragment instanceof FileDetailFragment) {
+            return (FileDetailFragment) fragment;
+        } else {
+            return null;
+        }
     }
 
     /**

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

@@ -288,7 +288,7 @@ public class FileDetailSharingFragment extends Fragment implements UserListAdapt
             if (TextUtils.isEmpty(file.getPublicLink())) {
                 fileOperationsHelper.getFileWithLink(file);
             } else {
-                fileDisplayActivity.showShareLinkDialog(file.getPublicLink());
+                FileDisplayActivity.showShareLinkDialog(fileDisplayActivity, file.getPublicLink());
             }
         }
     }
@@ -382,7 +382,7 @@ public class FileDetailSharingFragment extends Fragment implements UserListAdapt
             }
             case R.id.action_share_send_link: {
                 if(shareByLink.isChecked() && file.isSharedViaLink() && !TextUtils.isEmpty(file.getPublicLink())) {
-                    fileDisplayActivity.showShareLinkDialog(file.getPublicLink());
+                    FileDisplayActivity.showShareLinkDialog(fileDisplayActivity, file.getPublicLink());
                 } else {
                     showSendLinkTo();
                 }

+ 49 - 0
src/main/java/com/owncloud/android/ui/preview/PreviewImageActivity.java

@@ -31,6 +31,7 @@ import android.os.IBinder;
 import android.view.MenuItem;
 import android.view.View;
 
+import com.google.android.material.snackbar.Snackbar;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
@@ -45,12 +46,17 @@ import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
 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;
+import com.owncloud.android.lib.resources.shares.OCShare;
+import com.owncloud.android.operations.CreateShareViaLinkOperation;
 import com.owncloud.android.operations.RemoveFileOperation;
 import com.owncloud.android.operations.SynchronizeFileOperation;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.activity.FileDisplayActivity;
 import com.owncloud.android.ui.fragment.FileFragment;
+import com.owncloud.android.utils.ClipboardUtil;
+import com.owncloud.android.utils.ErrorMessageAdapter;
 import com.owncloud.android.utils.MimeTypeUtil;
+import com.owncloud.android.utils.ThemeUtils;
 
 import androidx.appcompat.app.ActionBar;
 import androidx.drawerlayout.widget.DrawerLayout;
@@ -178,10 +184,53 @@ public class PreviewImageActivity extends FileActivity implements
             finish();
         } else if (operation instanceof SynchronizeFileOperation) {
             onSynchronizeFileOperationFinish(result);
+        } else if (operation instanceof CreateShareViaLinkOperation) {
+            CreateShareViaLinkOperation op = (CreateShareViaLinkOperation) operation;
+
+            if (result.isSuccess()) {
+                updateFileFromDB();
+
+                // if share to user and share via link multiple ocshares are returned,
+                // therefore filtering for public_link
+                String link = "";
+                for (Object object : result.getData()) {
+                    OCShare shareLink = (OCShare) object;
+                    if (FileDisplayActivity.TAG_PUBLIC_LINK.equalsIgnoreCase(shareLink.getShareType().name())) {
+                        link = shareLink.getShareLink();
+                        break;
+                    }
+                }
 
+                copyAndShareFileLink(link);
+            } else {
+                // Detect Failure (403) --> maybe needs password
+                String password = op.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.
+
+                    Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content),
+                                                      ErrorMessageAdapter.getErrorCauseMessage(result,
+                                                                                               operation,
+                                                                                               getResources()),
+                                                      Snackbar.LENGTH_LONG);
+                    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(this, link));
+        ThemeUtils.colorSnackbar(this, snackbar);
+        snackbar.show();
+    }
+
     private void onSynchronizeFileOperationFinish(RemoteOperationResult result) {
         if (result.isSuccess()) {
             supportInvalidateOptionsMenu();