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

Fixed inconsistencies in file actions shown in menus according to the target file state

David A. Velasco 11 жил өмнө
parent
commit
288dbd6348

+ 88 - 103
src/com/owncloud/android/files/FileMenuFilter.java

@@ -1,3 +1,20 @@
+/* ownCloud Android client application
+ *   Copyright (C) 2014 ownCloud Inc.
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License version 2,
+ *   as published by the Free Software Foundation.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
 package com.owncloud.android.files;
 
 import java.util.ArrayList;
@@ -8,51 +25,55 @@ import android.content.Context;
 import android.view.Menu;
 import android.view.MenuItem;
 
-import com.actionbarsherlock.app.SherlockFragment;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
+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.ui.activity.ComponentsGetter;
-import com.owncloud.android.ui.fragment.FileDetailFragment;
-import com.owncloud.android.ui.fragment.OCFileListFragment;
-import com.owncloud.android.ui.preview.PreviewImageFragment;
-import com.owncloud.android.ui.preview.PreviewMediaFragment;
 
+/**
+ * Filters out the file actions available in a given {@link Menu} for a given {@link OCFile} 
+ * according to the current state of the latest. 
+ * 
+ * @author David A. Velasco
+ */
 public class FileMenuFilter {
 
     private OCFile mFile;
     private ComponentsGetter mComponentsGetter;
     private Account mAccount;
     private Context mContext;
-    private SherlockFragment mFragment;
-
-    public void setFile(OCFile targetFile) {
+    
+    /**
+     * Constructor
+     * 
+     * @param targetFile        {@link OCFile} target of the action to filter in the {@link Menu}.
+     * @param account           ownCloud {@link Account} holding targetFile.
+     * @param cg                Accessor to app components, needed to get access the 
+     *                          {@link FileUploader} and {@link FileDownloader} services.
+     * @param context           Android {@link Context}, needed to access build setup resources.
+     */
+    public FileMenuFilter(OCFile targetFile, Account account, ComponentsGetter cg, Context context) {
         mFile = targetFile;
-    }
-
-    public void setAccount(Account account) {
         mAccount = account;
-    }
-
-    public void setComponentGetter(ComponentsGetter cg) {
         mComponentsGetter = cg;
-    }
-
-    public void setContext(Context context) {
         mContext = context;
     }
     
-    public void setFragment(SherlockFragment fragment) {
-        mFragment = fragment;  
-    }
-
+    
+    /**
+     * Filters out the file actions available in the passed {@link Menu} taken into account
+     * the state of the {@link OCFile} held by the filter.
+     *  
+     * @param menu              Options or context menu to filter.
+     */
     public void filter(Menu menu) {
         List<Integer> toShow = new ArrayList<Integer>();  
-        List<Integer> toDisable = new ArrayList<Integer>();  
         List<Integer> toHide = new ArrayList<Integer>();    
         
-        filter(toShow, toDisable, toHide);
+        filter(toShow, toHide);
         
         MenuItem item = null;
         for (int i : toShow) {
@@ -63,14 +84,6 @@ public class FileMenuFilter {
             }
         }
         
-        for (int i : toDisable) {
-            item = menu.findItem(i);
-            if (item != null) {
-                item.setVisible(true);
-                item.setEnabled(false);
-            }
-        }
-        
         for (int i : toHide) {
             item = menu.findItem(i);
             if (item != null) {
@@ -81,16 +94,21 @@ public class FileMenuFilter {
     }
 
     /**
-     * ActionBarSherlock...
+     * Filters out the file actions available in the passed {@link Menu} taken into account
+     * the state of the {@link OCFile} held by the filter.
      * 
+     * Second method needed thanks to ActionBarSherlock.
+     * 
+     * TODO Get rid of it when ActionBarSherlock is replaced for newer Android Support Library.
+     *  
+     * @param menu              Options or context menu to filter.
      */
     public void filter(com.actionbarsherlock.view.Menu menu) {
 
         List<Integer> toShow = new ArrayList<Integer>();
-        List<Integer> toDisable = new ArrayList<Integer>(); 
         List<Integer> toHide = new ArrayList<Integer>();
         
-        filter(toShow, toDisable, toHide);
+        filter(toShow, toHide);
 
         com.actionbarsherlock.view.MenuItem item = null;
         for (int i : toShow) {
@@ -100,13 +118,6 @@ public class FileMenuFilter {
                 item.setEnabled(true);
             }
         }
-        for (int i : toDisable) {
-            item = menu.findItem(i);
-            if (item != null) {
-                item.setVisible(true);
-                item.setEnabled(false);
-            }
-        }
         for (int i : toHide) {
             item = menu.findItem(i);
             if (item != null) {
@@ -116,7 +127,15 @@ public class FileMenuFilter {
         }
     }
 
-    private void filter(List<Integer> toShow, List<Integer> toDisable, List <Integer> toHide) {
+    /**
+     * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
+     * 
+     * Decides what actions must be shown and hidden.
+     *  
+     * @param toShow            List to save the options that must be shown in the menu. 
+     * @param toHide            List to save the options that must be shown in the menu.
+     */
+    private void filter(List<Integer> toShow, List <Integer> toHide) {
         boolean downloading = false;
         boolean uploading = false;
         if (mComponentsGetter != null && mFile != null && mAccount != null) {
@@ -126,55 +145,34 @@ public class FileMenuFilter {
             uploading = uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile);
         }
         
-        // R.id.action_download_file
-        if (mFile == null || mFile.isFolder() || mFile.isDown() || downloading || uploading ||
-                (mFragment != null && (
-                        mFragment instanceof PreviewImageFragment ||
-                        mFragment instanceof PreviewMediaFragment
-                        )
-                )
-            ) {
+        /// decision is taken for each possible action on a file in the menu
+        
+        // DOWNLOAD 
+        if (mFile == null || mFile.isFolder() || mFile.isDown() || downloading || uploading) {
             toHide.add(R.id.action_download_file);
             
         } else {
             toShow.add(R.id.action_download_file);
         }
         
-        // R.id.action_rename_file
-        if ((downloading || uploading) && 
-                (mFragment != null && mFragment instanceof OCFileListFragment) 
-            ) {
-            toDisable.add(R.id.action_rename_file);
-            
-        } else if (mFile == null || downloading || uploading ||
-                (mFragment != null && (
-                        mFragment instanceof PreviewImageFragment ||
-                        mFragment instanceof PreviewMediaFragment
-                        )
-                ) 
-            ) {
+        // RENAME
+        if (mFile == null || downloading || uploading) {
             toHide.add(R.id.action_rename_file);
             
         } else {
             toShow.add(R.id.action_rename_file);
         }
         
-        // R.id.action_remove_file
-        if ((downloading || uploading) && 
-                (mFragment != null && mFragment instanceof OCFileListFragment) 
-            ) {
-            toDisable.add(R.id.action_remove_file);
-            
-        } else if (mFile == null || downloading || uploading) {
+        // REMOVE
+        if (mFile == null || downloading || uploading) {
             toHide.add(R.id.action_remove_file);
             
         } else {
             toShow.add(R.id.action_remove_file);
         }
         
-        // R.id.action_open_file_with
-        if (mFile == null || mFile.isFolder() || !mFile.isDown() || downloading || uploading || 
-                (mFragment != null && mFragment instanceof OCFileListFragment)) {
+        // OPEN WITH (different to preview!)
+        if (mFile == null || mFile.isFolder() || !mFile.isDown() || downloading || uploading) {
             toHide.add(R.id.action_open_file_with);
             
         } else {
@@ -182,49 +180,37 @@ public class FileMenuFilter {
         }
         
         
-        // R.id.action_cancel_download
-        if (mFile == null || !downloading || mFile.isFolder() || 
-                (mFragment != null && (
-                        (mFragment instanceof PreviewImageFragment) ||
-                        (mFragment instanceof PreviewMediaFragment)
-                        )
-                ) 
-            ) {
+        // CANCEL DOWNLOAD
+        if (mFile == null || !downloading || mFile.isFolder()) {
             toHide.add(R.id.action_cancel_download);
         } else {
             toShow.add(R.id.action_cancel_download);
         }
         
-        // R.id.action_cancel_upload
-        if (mFile == null || !uploading || mFile.isFolder() ||
-                (mFragment != null && (
-                        (mFragment instanceof PreviewImageFragment) ||
-                        (mFragment instanceof PreviewMediaFragment)
-                        )
-                ) 
-            ) {
+        // CANCEL UPLOAD
+        if (mFile == null || !uploading || mFile.isFolder()) {
             toHide.add(R.id.action_cancel_upload);
         } else {
             toShow.add(R.id.action_cancel_upload);
         }
         
-        // R.id.action_sync_file
-        if (mFile == null || mFile.isFolder() || !mFile.isDown() || downloading || uploading ||
-                (mFragment != null && mFragment instanceof PreviewMediaFragment)
-            ) {
+        // SYNC FILE CONTENTS
+        if (mFile == null || mFile.isFolder() || !mFile.isDown() || downloading || uploading) {
             toHide.add(R.id.action_sync_file);
         } else {
             toShow.add(R.id.action_sync_file);
         }
         
-        // R.id.action_share_file  // TODO add check on SHARE available on server side?
+        // SHARE FILE 
+        // TODO add check on SHARE available on server side?
         if (mFile == null) {
             toHide.add(R.id.action_share_file);
         } else {
             toShow.add(R.id.action_share_file);
         }
         
-        // R.id.action_unshare_file  // TODO add check on SHARE available on server side?
+        // UNSHARE FILE  
+        // TODO add check on SHARE available on server side?
         if (mFile == null || !mFile.isShareByLink()) { 
             toHide.add(R.id.action_unshare_file);
         } else {
@@ -232,21 +218,20 @@ public class FileMenuFilter {
         }
         
         
-        // R.id.action_see_details
-        if (mFile == null || mFile.isFolder() || (mFragment != null && mFragment instanceof FileDetailFragment)) {
-            // TODO check dual pane when FileDetailFragment is shown
+        // SEE DETAILS
+        if (mFile == null || mFile.isFolder()) {
             toHide.add(R.id.action_see_details);
         } else {
             toShow.add(R.id.action_see_details);
         }
         
-        // R.id.action_send_file
-        boolean sendEnabled = (mContext != null &&
+        // SEND
+        boolean sendAllowed = (mContext != null &&
                 mContext.getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on"));
-        if (mFile != null && sendEnabled && !mFile.isFolder()) {
-            toShow.add(R.id.action_send_file);
-        } else {
+        if (mFile == null || !sendAllowed || mFile.isFolder() || uploading || downloading) {
             toHide.add(R.id.action_send_file);
+        } else {
+            toShow.add(R.id.action_send_file);
         }
 
     }

+ 13 - 1
src/com/owncloud/android/operations/DownloadFileOperation.java

@@ -21,10 +21,12 @@ import java.io.File;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
 import com.owncloud.android.lib.common.OwnCloudClient;
+import com.owncloud.android.lib.common.operations.OperationCancelledException;
 import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation;
@@ -48,6 +50,7 @@ public class DownloadFileOperation extends RemoteOperation {
     private OCFile mFile;
     private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
     private long mModificationTimestamp = 0;
+    private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
     
     private DownloadRemoteFileOperation mDownloadOperation;
 
@@ -129,6 +132,12 @@ public class DownloadFileOperation extends RemoteOperation {
         String tmpFolder =  getTmpFolder();
         
         /// perform the download
+        synchronized(mCancellationRequested) {
+            if (mCancellationRequested.get()) {
+                return new RemoteOperationResult(new OperationCancelledException());
+            }
+        }
+        
         mDownloadOperation = new DownloadRemoteFileOperation(mFile.getRemotePath(), tmpFolder);
         Iterator<OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
         while (listener.hasNext()) {
@@ -152,7 +161,10 @@ public class DownloadFileOperation extends RemoteOperation {
     }
 
     public void cancel() {
-        mDownloadOperation.cancel();
+        mCancellationRequested.set(true);   // atomic set; there is no need of synchronizing it
+        if (mDownloadOperation != null) {
+            mDownloadOperation.cancel();
+        }
     }
 
 

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

@@ -378,7 +378,7 @@ OnSslUntrustedCertListener, EditNameDialogListener {
         return null;
     }
 
-    protected FileFragment getSecondFragment() {
+    public FileFragment getSecondFragment() {
         Fragment second = getSupportFragmentManager().findFragmentByTag(FileDisplayActivity.TAG_SECOND_FRAGMENT);
         if (second != null) {
             return (FileFragment)second;
@@ -1563,10 +1563,12 @@ OnSslUntrustedCertListener, EditNameDialogListener {
         Account account = getAccount();
         if (mDownloaderBinder != null && mDownloaderBinder.isDownloading(account, file)) {
             mDownloaderBinder.cancel(account, file);
+            refreshListOfFilesFragment();
             onTransferStateChanged(file, false, false);
 
         } else if (mUploaderBinder != null && mUploaderBinder.isUploading(account, file)) {
             mUploaderBinder.cancel(account, file);
+            refreshListOfFilesFragment();
             if (!file.fileExists()) {
                 cleanSecondFragment();
                 

+ 16 - 33
src/com/owncloud/android/ui/fragment/FileDetailFragment.java

@@ -69,6 +69,7 @@ public class FileDetailFragment extends FileFragment implements
     
     private static final String TAG = FileDetailFragment.class.getSimpleName();
     public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";
+    public static final String FTAG_RENAME_FILE = "RENAME_FILE_FRAGMENT";
     
 
     /**
@@ -109,7 +110,6 @@ public class FileDetailFragment extends FileFragment implements
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
-        //super.onCreateView(inflater, container, savedInstanceState);
         
         if (savedInstanceState != null) {
             setFile((OCFile)savedInstanceState.getParcelable(FileActivity.EXTRA_FILE));
@@ -168,31 +168,7 @@ public class FileDetailFragment extends FileFragment implements
     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
         super.onCreateOptionsMenu(menu, inflater);
         inflater.inflate(R.menu.file_actions_menu, menu);
-        
-        /*
-         TODO Maybe should stay here? It's context (fragment) specific 
-          
-        MenuItem item = menu.findItem(R.id.action_see_details);
-        if (item != null) {
-            item.setVisible(false);
-            item.setEnabled(false);
-        }
-        
-        // Send file
-        item = menu.findItem(R.id.action_send_file);
-        boolean sendEnabled = getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on");
-        if (item != null) {
-            if (sendEnabled) {
-                item.setVisible(true);
-                item.setEnabled(true);
-            } else {
-                item.setVisible(false);
-                item.setEnabled(false);
-                
-            }
-        }
-        */
-    }
+   }
 
     
     /**
@@ -202,13 +178,20 @@ public class FileDetailFragment extends FileFragment implements
     public void onPrepareOptionsMenu (Menu menu) {
         super.onPrepareOptionsMenu(menu);
         
-        FileMenuFilter mf = new FileMenuFilter();
-        mf.setFile(getFile());
-        mf.setComponentGetter(mContainerActivity);
-        mf.setAccount(mContainerActivity.getStorageManager().getAccount());
-        mf.setContext(getSherlockActivity());
-        mf.setFragment(this);
+        FileMenuFilter mf = new FileMenuFilter(
+            getFile(),
+            mContainerActivity.getStorageManager().getAccount(),
+            mContainerActivity,
+            getSherlockActivity()
+        );
         mf.filter(menu);
+        
+        // additional restriction for this fragment 
+        MenuItem item = menu.findItem(R.id.action_see_details);
+        if (item != null) {
+            item.setVisible(false);
+            item.setEnabled(false);
+        }
     }
 
     
@@ -323,7 +306,7 @@ public class FileDetailFragment extends FileFragment implements
         int extensionStart = file.isFolder() ? -1 : fileName.lastIndexOf(".");
         int selectionEnd = (extensionStart >= 0) ? extensionStart : fileName.length();
         EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.rename_dialog_title), fileName, 0, selectionEnd, this);
-        dialog.show(getFragmentManager(), "nameeditdialog");
+        dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
     }
 
     

+ 30 - 28
src/com/owncloud/android/ui/fragment/OCFileListFragment.java

@@ -21,12 +21,9 @@ import java.io.File;
 import java.util.ArrayList;
 
 import com.owncloud.android.R;
-import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.FileMenuFilter;
-import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
-import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.ui.adapter.FileListListAdapter;
 import com.owncloud.android.ui.activity.FileDisplayActivity;
 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
@@ -37,7 +34,6 @@ import com.owncloud.android.ui.preview.PreviewImageFragment;
 import com.owncloud.android.ui.preview.PreviewMediaFragment;
 import com.owncloud.android.utils.Log_OC;
 
-import android.accounts.Account;
 import android.app.Activity;
 import android.os.Bundle;
 import android.view.ContextMenu;
@@ -299,13 +295,33 @@ implements EditNameDialogListener, ConfirmationDialogFragmentListener {
         AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
         OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
         
-        FileMenuFilter mf = new FileMenuFilter();
-        mf.setFile(targetFile);
-        mf.setComponentGetter(mContainerActivity);
-        mf.setAccount(mContainerActivity.getStorageManager().getAccount());
-        mf.setContext(getSherlockActivity());
-        mf.setFragment(this);
+        FileMenuFilter mf = new FileMenuFilter(
+            targetFile,
+            mContainerActivity.getStorageManager().getAccount(),
+            mContainerActivity,
+            getSherlockActivity()
+        );
         mf.filter(menu);
+        
+        /// additional restrictions for this fragment 
+        // TODO allow in the future 'open with' for previewable files
+        MenuItem item = menu.findItem(R.id.action_open_file_with);
+        if (item != null) {
+            item.setVisible(false);
+            item.setEnabled(false);
+        }
+        /// TODO break this direct dependency on FileDisplayActivity... if possible
+        FileFragment frag = ((FileDisplayActivity)getSherlockActivity()).getSecondFragment();
+        if (frag != null && frag instanceof FileDetailFragment && 
+                frag.getFile().getFileId() == targetFile.getFileId()) {
+            item = menu.findItem(R.id.action_see_details);
+            if (item != null) {
+                item.setVisible(false);
+                item.setEnabled(false);
+            }
+        }
+        
+
     }
     
     
@@ -330,7 +346,7 @@ implements EditNameDialogListener, ConfirmationDialogFragmentListener {
                 int extensionStart = mTargetFile.isFolder() ? -1 : fileName.lastIndexOf(".");
                 int selectionEnd = (extensionStart >= 0) ? extensionStart : fileName.length();
                 EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.rename_dialog_title), fileName, 0, selectionEnd, this);
-                dialog.show(getFragmentManager(), EditNameDialog.TAG);
+                dialog.show(getFragmentManager(), FileDetailFragment.FTAG_RENAME_FILE);
                 return true;
             }
             case R.id.action_remove_file: {
@@ -355,28 +371,14 @@ implements EditNameDialogListener, ConfirmationDialogFragmentListener {
                 confDialog.show(getFragmentManager(), FileDetailFragment.FTAG_CONFIRMATION);
                 return true;
             }
+            case R.id.action_download_file: 
             case R.id.action_sync_file: {
                 mContainerActivity.getFileOperationsHelper().syncFile(mTargetFile);
                 return true;
             }
-            case R.id.action_cancel_download: {
-                FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
-                Account account = AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity());
-                if (downloaderBinder != null && downloaderBinder.isDownloading(account, mTargetFile)) {
-                    downloaderBinder.cancel(account, mTargetFile);
-                    listDirectory();
-                    mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
-                }
-                return true;
-            }
+            case R.id.action_cancel_download:
             case R.id.action_cancel_upload: {
-                FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
-                Account account = AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity());
-                if (uploaderBinder != null && uploaderBinder.isUploading(account, mTargetFile)) {
-                    uploaderBinder.cancel(account, mTargetFile);
-                    listDirectory();
-                    mContainerActivity.onTransferStateChanged(mTargetFile, false, false);
-                }
+                ((FileDisplayActivity)mContainerActivity).cancelTransference(mTargetFile);
                 return true;
             }
             case R.id.action_see_details: {

+ 15 - 38
src/com/owncloud/android/ui/preview/PreviewImageFragment.java

@@ -216,30 +216,7 @@ ConfirmationDialogFragment.ConfirmationDialogFragmentListener {
     @Override
     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
         super.onCreateOptionsMenu(menu, inflater);
-
         inflater.inflate(R.menu.file_actions_menu, menu);
-        /*List<Integer> toHide = new ArrayList<Integer>();    
-        MenuItem item = null;
-        toHide.add(R.id.action_cancel_download);
-        toHide.add(R.id.action_cancel_upload);
-        toHide.add(R.id.action_download_file);
-        toHide.add(R.id.action_rename_file);    // by now
-        
-        // Send file
-        boolean sendEnabled = getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on");
-        if (!sendEnabled) {
-            toHide.add(R.id.action_send_file);
-        }
-        
-        for (int i : toHide) {
-            item = menu.findItem(i);
-            if (item != null) {
-                item.setVisible(false);
-                item.setEnabled(false);
-            }
-        }
-        */
-        
     }
 
     /**
@@ -249,25 +226,21 @@ ConfirmationDialogFragment.ConfirmationDialogFragmentListener {
     public void onPrepareOptionsMenu(Menu menu) {
         super.onPrepareOptionsMenu(menu);
         
-        FileMenuFilter mf = new FileMenuFilter();
-        mf.setFile(getFile());
-        mf.setComponentGetter(mContainerActivity);
-        mf.setAccount(mContainerActivity.getStorageManager().getAccount());
-        mf.setContext(getSherlockActivity());
-        mf.setFragment(this);
+        FileMenuFilter mf = new FileMenuFilter(
+            getFile(),
+            mContainerActivity.getStorageManager().getAccount(),
+            mContainerActivity,
+            getSherlockActivity()
+        );
         mf.filter(menu);
-
-        /*
-        MenuItem item = menu.findItem(R.id.action_unshare_file);
-        // Options shareLink
-        if (!getFile().isShareByLink()) {
+        
+        // additional restriction for this fragment 
+        // TODO allow renaming in PreviewImageFragment
+        MenuItem item = menu.findItem(R.id.action_rename_file);
+        if (item != null) {
             item.setVisible(false);
             item.setEnabled(false);
-        } else {
-            item.setVisible(true);
-            item.setEnabled(true);
         }
-        */
     }
 
     
@@ -302,6 +275,10 @@ ConfirmationDialogFragment.ConfirmationDialogFragmentListener {
                 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
                 return true;
             }
+            case R.id.action_sync_file: {
+                mContainerActivity.getFileOperationsHelper().syncFile(getFile());
+                return true;
+            }
             
             default:
                 return false;

+ 19 - 59
src/com/owncloud/android/ui/preview/PreviewMediaFragment.java

@@ -246,38 +246,7 @@ public class PreviewMediaFragment extends FileFragment implements
     @Override
     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
         super.onCreateOptionsMenu(menu, inflater);
-
         inflater.inflate(R.menu.file_actions_menu, menu);
-        /*
-        List<Integer> toHide = new ArrayList<Integer>();    
-        
-        MenuItem item = null;
-        toHide.add(R.id.action_cancel_download);
-        toHide.add(R.id.action_cancel_upload);
-        toHide.add(R.id.action_download_file);
-        toHide.add(R.id.action_sync_file);
-        toHide.add(R.id.action_rename_file);    // by now
-        
-        // Options shareLink
-        if (!getFile().isShareByLink()) {
-            toHide.add(R.id.action_unshare_file);
-        }
-        
-        // Send file
-        boolean sendEnabled = getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on");
-        if (!sendEnabled) {
-            toHide.add(R.id.action_send_file);
-        }
-        
-        for (int i : toHide) {
-            item = menu.findItem(i);
-            if (item != null) {
-                item.setVisible(false);
-                item.setEnabled(false);
-            }
-        }
-        */
-        
     }
 
 
@@ -288,25 +257,21 @@ public class PreviewMediaFragment extends FileFragment implements
     public void onPrepareOptionsMenu(Menu menu) {
         super.onPrepareOptionsMenu(menu);
         
-        FileMenuFilter mf = new FileMenuFilter();
-        mf.setFile(getFile());
-        mf.setComponentGetter(mContainerActivity);
-        mf.setAccount(mContainerActivity.getStorageManager().getAccount());
-        mf.setContext(getSherlockActivity());
-        mf.setFragment(this);
+        FileMenuFilter mf = new FileMenuFilter(
+            getFile(),
+            mContainerActivity.getStorageManager().getAccount(),
+            mContainerActivity,
+            getSherlockActivity()
+        );
         mf.filter(menu);
 
-        /*
-        MenuItem item = menu.findItem(R.id.action_unshare_file);
-        // Options shareLink
-        if (!getFile().isShareByLink()) {            
+        // additional restriction for this fragment 
+        // TODO allow renaming in PreviewImageFragment
+        MenuItem item = menu.findItem(R.id.action_rename_file);
+        if (item != null) {
             item.setVisible(false);
             item.setEnabled(false);
-        } else {
-            item.setVisible(true);
-            item.setEnabled(true);
         }
-        */
     }
     
     
@@ -317,11 +282,13 @@ public class PreviewMediaFragment extends FileFragment implements
     public boolean onOptionsItemSelected(MenuItem item) {
         switch (item.getItemId()) {
             case R.id.action_share_file: {
-                shareFileWithLink();
+                stopPreview(false);
+                mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
                 return true;
             }
             case R.id.action_unshare_file: {
-                unshareFileWithLink();
+                stopPreview(false);
+                mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
                 return true;
             }
             case R.id.action_open_file_with: {
@@ -339,7 +306,11 @@ public class PreviewMediaFragment extends FileFragment implements
             case R.id.action_send_file: {
                 sendFile();
             }
-            
+            case R.id.action_sync_file: {
+                mContainerActivity.getFileOperationsHelper().syncFile(getFile());
+                return true;
+            }
+
             default:
                 return false;
         }
@@ -355,17 +326,6 @@ public class PreviewMediaFragment extends FileFragment implements
         setFile(file);
     }
     
-    private void unshareFileWithLink() {
-        stopPreview(false);
-        mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
-    }
-    
-    private void shareFileWithLink() {
-        stopPreview(false);
-        mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
-        
-    }
-
     private void sendFile() {
         stopPreview(false);
         mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());