Przeglądaj źródła

Refactored menu filtering in a single class FileMenuFilter

David A. Velasco 11 lat temu
rodzic
commit
2f2570bbc5

+ 254 - 0
src/com/owncloud/android/files/FileMenuFilter.java

@@ -0,0 +1,254 @@
+package com.owncloud.android.files;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import android.accounts.Account;
+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.FileDownloaderBinder;
+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;
+
+public class FileMenuFilter {
+
+    private OCFile mFile;
+    private ComponentsGetter mComponentsGetter;
+    private Account mAccount;
+    private Context mContext;
+    private SherlockFragment mFragment;
+
+    public void setFile(OCFile targetFile) {
+        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;  
+    }
+
+    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);
+        
+        MenuItem item = null;
+        for (int i : toShow) {
+            item = menu.findItem(i);
+            if (item != null) {
+                item.setVisible(true);
+                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) {
+                item.setVisible(false);
+                item.setEnabled(false);
+            }
+        }
+    }
+
+    /**
+     * ActionBarSherlock...
+     * 
+     */
+    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);
+
+        com.actionbarsherlock.view.MenuItem item = null;
+        for (int i : toShow) {
+            item = menu.findItem(i);
+            if (item != null) {
+                item.setVisible(true);
+                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) {
+                item.setVisible(false);
+                item.setEnabled(false);
+            }
+        }
+    }
+
+    private void filter(List<Integer> toShow, List<Integer> toDisable, List <Integer> toHide) {
+        boolean downloading = false;
+        boolean uploading = false;
+        if (mComponentsGetter != null && mFile != null && mAccount != null) {
+            FileDownloaderBinder downloaderBinder = mComponentsGetter.getFileDownloaderBinder();
+            downloading = downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile);
+            FileUploaderBinder uploaderBinder = mComponentsGetter.getFileUploaderBinder();
+            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
+                        )
+                )
+            ) {
+            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
+                        )
+                ) 
+            ) {
+            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) {
+            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)) {
+            toHide.add(R.id.action_open_file_with);
+            
+        } else {
+            toShow.add(R.id.action_open_file_with);
+        }
+        
+        
+        // R.id.action_cancel_download
+        if (mFile == null || !downloading || mFile.isFolder() || 
+                (mFragment != null && (
+                        (mFragment instanceof PreviewImageFragment) ||
+                        (mFragment instanceof PreviewMediaFragment)
+                        )
+                ) 
+            ) {
+            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)
+                        )
+                ) 
+            ) {
+            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)
+            ) {
+            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?
+        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?
+        if (mFile == null || !mFile.isShareByLink()) { 
+            toHide.add(R.id.action_unshare_file);
+        } else {
+            toShow.add(R.id.action_unshare_file);
+        }
+        
+        
+        // R.id.action_see_details
+        if (mFile == null || mFile.isFolder() || (mFragment != null && mFragment instanceof FileDetailFragment)) {
+            // TODO check dual pane when FileDetailFragment is shown
+            toHide.add(R.id.action_see_details);
+        } else {
+            toShow.add(R.id.action_see_details);
+        }
+        
+        // R.id.action_send_file
+        boolean sendEnabled = (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 {
+            toHide.add(R.id.action_send_file);
+        }
+
+    }
+
+}

+ 13 - 94
src/com/owncloud/android/ui/fragment/FileDetailFragment.java

@@ -18,8 +18,6 @@
 package com.owncloud.android.ui.fragment;
 
 import java.lang.ref.WeakReference;
-import java.util.ArrayList;
-import java.util.List;
 
 import android.accounts.Account;
 import android.content.Intent;
@@ -39,6 +37,7 @@ import com.actionbarsherlock.view.MenuItem;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.files.FileMenuFilter;
 import com.owncloud.android.files.services.FileObserverService;
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
@@ -136,23 +135,6 @@ public class FileDetailFragment extends FileFragment implements
         return view;
     }
 
-    /*-*
-     * {@inheritDoc}
-     *-/
-    @Override
-    public void onActivityCreated(Bundle savedInstanceState) {
-        super.onActivityCreated(savedInstanceState);
-        if (mAccount != null) {
-            OCFile file = ((FileActivity)getActivity()).getStorageManager().
-                    getFileByPath(getFile().getRemotePath());
-            if (file != null) {
-                setFile(file);
-            }
-        }
-    }
-    */
-        
-
     @Override
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
@@ -186,6 +168,10 @@ 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);
@@ -205,6 +191,7 @@ public class FileDetailFragment extends FileFragment implements
                 
             }
         }
+        */
     }
 
     
@@ -215,81 +202,13 @@ public class FileDetailFragment extends FileFragment implements
     public void onPrepareOptionsMenu (Menu menu) {
         super.onPrepareOptionsMenu(menu);
         
-        List<Integer> toHide = new ArrayList<Integer>();
-        List<Integer> toShow = new ArrayList<Integer>();
-        OCFile file = getFile();
-        
-        FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
-        boolean downloading = downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file);
-        FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
-        boolean uploading = uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile());
-        
-        if (downloading || uploading) {
-            toHide.add(R.id.action_download_file);
-            toHide.add(R.id.action_rename_file);
-            toHide.add(R.id.action_remove_file);
-            toHide.add(R.id.action_open_file_with);
-            if (!downloading) {
-                toHide.add(R.id.action_cancel_download);
-                toShow.add(R.id.action_cancel_upload);
-            } else {
-                toHide.add(R.id.action_cancel_upload);
-                toShow.add(R.id.action_cancel_download);
-            }
-
-        } else if (file != null && file.isDown()) {
-            toHide.add(R.id.action_download_file);
-            toHide.add(R.id.action_cancel_download);
-            toHide.add(R.id.action_cancel_upload);
-            
-            toShow.add(R.id.action_rename_file);
-            toShow.add(R.id.action_remove_file);
-            toShow.add(R.id.action_open_file_with);
-            toShow.add(R.id.action_sync_file);
-            
-        } else if (file != null) {
-            toHide.add(R.id.action_open_file_with);
-            toHide.add(R.id.action_cancel_download);
-            toHide.add(R.id.action_cancel_upload);
-            toHide.add(R.id.action_sync_file);
-            
-            toShow.add(R.id.action_rename_file);
-            toShow.add(R.id.action_remove_file);
-            toShow.add(R.id.action_download_file);
-            
-        } else {
-            toHide.add(R.id.action_open_file_with);
-            toHide.add(R.id.action_cancel_download);
-            toHide.add(R.id.action_cancel_upload);
-            toHide.add(R.id.action_sync_file);
-            toHide.add(R.id.action_download_file);
-            toHide.add(R.id.action_rename_file);
-            toHide.add(R.id.action_remove_file);
-            
-        }
-        
-        // Options shareLink
-        if (!file.isShareByLink()) {
-            toHide.add(R.id.action_unshare_file);
-        } else {
-            toShow.add(R.id.action_unshare_file);
-        }
-        
-        MenuItem item = null;
-        for (int i : toHide) {
-            item = menu.findItem(i);
-            if (item != null) {
-                item.setVisible(false);
-                item.setEnabled(false);
-            }
-        }
-        for (int i : toShow) {
-            item = menu.findItem(i);
-            if (item != null) {
-                item.setVisible(true);
-                item.setEnabled(true);
-            }
-        }
+        FileMenuFilter mf = new FileMenuFilter();
+        mf.setFile(getFile());
+        mf.setComponentGetter(mContainerActivity);
+        mf.setAccount(mContainerActivity.getStorageManager().getAccount());
+        mf.setContext(getSherlockActivity());
+        mf.setFragment(this);
+        mf.filter(menu);
     }
 
     

+ 10 - 76
src/com/owncloud/android/ui/fragment/OCFileListFragment.java

@@ -19,12 +19,12 @@ package com.owncloud.android.ui.fragment;
 
 import java.io.File;
 import java.util.ArrayList;
-import java.util.List;
 
 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;
@@ -56,7 +56,8 @@ import android.widget.AdapterView.AdapterContextMenuInfo;
  * @author masensio
  * @author David A. Velasco
  */
-public class OCFileListFragment extends ExtendedListFragment implements EditNameDialogListener, ConfirmationDialogFragmentListener {
+public class OCFileListFragment extends ExtendedListFragment 
+implements EditNameDialogListener, ConfirmationDialogFragmentListener {
     
     private static final String TAG = OCFileListFragment.class.getSimpleName();
 
@@ -297,81 +298,14 @@ public class OCFileListFragment extends ExtendedListFragment implements EditName
         inflater.inflate(R.menu.file_actions_menu, menu);
         AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
         OCFile targetFile = (OCFile) mAdapter.getItem(info.position);
-        List<Integer> toHide = new ArrayList<Integer>();    
-        List<Integer> toDisable = new ArrayList<Integer>();  
         
-        MenuItem item = null;
-        if (targetFile.isFolder()) {
-            // contextual menu for folders
-            toHide.add(R.id.action_open_file_with);
-            toHide.add(R.id.action_download_file);
-            toHide.add(R.id.action_cancel_download);
-            toHide.add(R.id.action_cancel_upload);
-            toHide.add(R.id.action_sync_file);
-            toHide.add(R.id.action_see_details);
-            toHide.add(R.id.action_send_file);
-            if (    mContainerActivity.getFileDownloaderBinder().isDownloading(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile) ||
-                    mContainerActivity.getFileUploaderBinder().isUploading(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile)           ) {
-                toDisable.add(R.id.action_rename_file);
-                toDisable.add(R.id.action_remove_file);
-                
-            }
-            
-        } else {
-            // contextual menu for regular files
-            
-            // new design: 'download' and 'open with' won't be available anymore in context menu
-            toHide.add(R.id.action_download_file);
-            toHide.add(R.id.action_open_file_with);
-            
-            if (targetFile.isDown()) {
-                toHide.add(R.id.action_cancel_download);
-                toHide.add(R.id.action_cancel_upload);
-                
-            } else {
-                toHide.add(R.id.action_sync_file);
-            }
-            if ( mContainerActivity.getFileDownloaderBinder().isDownloading(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile)) {
-                toHide.add(R.id.action_cancel_upload);
-                toDisable.add(R.id.action_rename_file);
-                toDisable.add(R.id.action_remove_file);
-                    
-            } else if ( mContainerActivity.getFileUploaderBinder().isUploading(AccountUtils.getCurrentOwnCloudAccount(getSherlockActivity()), targetFile)) {
-                toHide.add(R.id.action_cancel_download);
-                toDisable.add(R.id.action_rename_file);
-                toDisable.add(R.id.action_remove_file);
-                    
-            } else {
-                toHide.add(R.id.action_cancel_download);
-                toHide.add(R.id.action_cancel_upload);
-            }
-        }
-        
-        // Options shareLink
-        if (!targetFile.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);
-            }
-        }
-        
-        for (int i : toDisable) {
-            item = menu.findItem(i);
-            if (item != null) {
-                item.setEnabled(false);
-            }
-        }
+        FileMenuFilter mf = new FileMenuFilter();
+        mf.setFile(targetFile);
+        mf.setComponentGetter(mContainerActivity);
+        mf.setAccount(mContainerActivity.getStorageManager().getAccount());
+        mf.setContext(getSherlockActivity());
+        mf.setFragment(this);
+        mf.filter(menu);
     }
     
     

+ 13 - 10
src/com/owncloud/android/ui/preview/PreviewImageFragment.java

@@ -17,8 +17,6 @@
 package com.owncloud.android.ui.preview;
 
 import java.lang.ref.WeakReference;
-import java.util.ArrayList;
-import java.util.List;
 
 
 import android.accounts.Account;
@@ -46,6 +44,7 @@ import com.actionbarsherlock.view.MenuItem;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.files.FileMenuFilter;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
 import com.owncloud.android.ui.fragment.FileFragment;
@@ -219,19 +218,13 @@ ConfirmationDialogFragment.ConfirmationDialogFragmentListener {
         super.onCreateOptionsMenu(menu, inflater);
 
         inflater.inflate(R.menu.file_actions_menu, menu);
-        List<Integer> toHide = new ArrayList<Integer>();    
-        
+        /*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
         
-        // 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) {
@@ -245,6 +238,7 @@ ConfirmationDialogFragment.ConfirmationDialogFragmentListener {
                 item.setEnabled(false);
             }
         }
+        */
         
     }
 
@@ -255,6 +249,15 @@ 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);
+        mf.filter(menu);
+
+        /*
         MenuItem item = menu.findItem(R.id.action_unshare_file);
         // Options shareLink
         if (!getFile().isShareByLink()) {
@@ -264,7 +267,7 @@ ConfirmationDialogFragment.ConfirmationDialogFragmentListener {
             item.setVisible(true);
             item.setEnabled(true);
         }
-            
+        */
     }
 
     

+ 13 - 3
src/com/owncloud/android/ui/preview/PreviewMediaFragment.java

@@ -16,9 +16,6 @@
  */
 package com.owncloud.android.ui.preview;
 
-import java.util.ArrayList;
-import java.util.List;
-
 import android.accounts.Account;
 import android.app.Activity;
 import android.app.AlertDialog;
@@ -50,6 +47,7 @@ import com.actionbarsherlock.view.MenuItem;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.files.FileMenuFilter;
 import com.owncloud.android.media.MediaControlView;
 import com.owncloud.android.media.MediaService;
 import com.owncloud.android.media.MediaServiceBinder;
@@ -250,6 +248,7 @@ public class PreviewMediaFragment extends FileFragment implements
         super.onCreateOptionsMenu(menu, inflater);
 
         inflater.inflate(R.menu.file_actions_menu, menu);
+        /*
         List<Integer> toHide = new ArrayList<Integer>();    
         
         MenuItem item = null;
@@ -277,6 +276,7 @@ public class PreviewMediaFragment extends FileFragment implements
                 item.setEnabled(false);
             }
         }
+        */
         
     }
 
@@ -288,6 +288,15 @@ 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);
+        mf.filter(menu);
+
+        /*
         MenuItem item = menu.findItem(R.id.action_unshare_file);
         // Options shareLink
         if (!getFile().isShareByLink()) {            
@@ -297,6 +306,7 @@ public class PreviewMediaFragment extends FileFragment implements
             item.setVisible(true);
             item.setEnabled(true);
         }
+        */
     }