Procházet zdrojové kódy

Update menu filter on item checked state changed

Juan Carlos González Cabrero před 9 roky
rodič
revize
217a5e9825

+ 24 - 8
src/com/owncloud/android/files/FileMenuFilter.java

@@ -47,6 +47,7 @@ public class FileMenuFilter {
     private ComponentsGetter mComponentsGetter;
     private Account mAccount;
     private Context mContext;
+    private boolean mMultiSelect;
 
     /**
      * Constructor
@@ -56,15 +57,30 @@ public class FileMenuFilter {
      * @param cg                Accessor to app components, needed to access the
      *                          {@link FileUploader} and {@link FileDownloader} services
      * @param context           Android {@link Context}, needed to access build setup resources.
+     * @param multiSelect       Flag that indicates if a multiSelect mode is activated.
      */
     public FileMenuFilter(OCFile targetFile, Account account, ComponentsGetter cg,
-                          Context context) {
+                          Context context, boolean multiSelect) {
         mFile = targetFile;
         mAccount = account;
         mComponentsGetter = cg;
         mContext = context;
+        mMultiSelect = multiSelect;
     }
 
+    /**
+     * 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 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) {
+        this(targetFile, account, cg, context, false);
+    }
 
     /**
      * Filters out the file actions available in the passed {@link Menu} taken into account
@@ -132,7 +148,7 @@ public class FileMenuFilter {
         }
 
         // RENAME
-        if (mFile == null || synchronizing) {
+        if (mFile == null || synchronizing || mMultiSelect) {
             toHide.add(R.id.action_rename_file);
 
         } else {
@@ -157,7 +173,7 @@ public class FileMenuFilter {
         }
 
         // OPEN WITH (different to preview!)
-        if (mFile == null || mFile.isFolder() || !mFile.isDown() || synchronizing) {
+        if (mFile == null || mFile.isFolder() || !mFile.isDown() || synchronizing || mMultiSelect) {
             toHide.add(R.id.action_open_file_with);
 
         } else {
@@ -191,14 +207,14 @@ public class FileMenuFilter {
                 (capability.getFilesSharingApiEnabled().isTrue() ||
                         capability.getFilesSharingApiEnabled().isUnknown()
                 );
-        if ((!shareViaLinkAllowed && !shareWithUsersAllowed) ||  mFile == null || !shareApiEnabled) {
+        if ((!shareViaLinkAllowed && !shareWithUsersAllowed) ||  mFile == null || !shareApiEnabled || mMultiSelect) {
             toHide.add(R.id.action_share_file);
         } else {
             toShow.add(R.id.action_share_file);
         }
 
         // SEE DETAILS
-        if (mFile == null || mFile.isFolder()) {
+        if (mFile == null || mFile.isFolder() || mMultiSelect) {
             toHide.add(R.id.action_see_details);
         } else {
             toShow.add(R.id.action_see_details);
@@ -207,21 +223,21 @@ public class FileMenuFilter {
         // SEND
         boolean sendAllowed = (mContext != null &&
                 mContext.getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on"));
-        if (mFile == null || !sendAllowed || mFile.isFolder() || synchronizing) {
+        if (mFile == null || !sendAllowed || mFile.isFolder() || synchronizing || mMultiSelect) {
             toHide.add(R.id.action_send_file);
         } else {
             toShow.add(R.id.action_send_file);
         }
 
         // FAVORITES
-        if (mFile == null || synchronizing || mFile.isFolder() || mFile.isFavorite()) {
+        if (mFile == null || synchronizing || mFile.isFolder() || mFile.isFavorite() || mMultiSelect) {
             toHide.add(R.id.action_favorite_file);
         } else {
             toShow.add(R.id.action_favorite_file);
         }
 
         // UNFAVORITES
-        if (mFile == null || synchronizing || mFile.isFolder() || !mFile.isFavorite()) {
+        if (mFile == null || synchronizing || mFile.isFolder() || !mFile.isFavorite() || mMultiSelect) {
             toHide.add(R.id.action_unfavorite_file);
         } else {
             toShow.add(R.id.action_unfavorite_file);

+ 29 - 41
src/com/owncloud/android/ui/fragment/OCFileListFragment.java

@@ -96,6 +96,8 @@ public class OCFileListFragment extends ExtendedListFragment {
 
     private static String DIALOG_CREATE_FOLDER = "DIALOG_CREATE_FOLDER";
 
+    private static final int MIN_FILES_FOR_MULTISELECT = 2;
+
     private FileFragment.ContainerActivity mContainerActivity;
 
     private OCFile mFile = null;
@@ -347,7 +349,9 @@ public class OCFileListFragment extends ExtendedListFragment {
         AbsListView listView = getListView();
         setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
         setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
+
             private Menu menu;
+
             @Override
             public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                 final int checkedCount = getListView().getCheckedItemCount();
@@ -360,20 +364,34 @@ public class OCFileListFragment extends ExtendedListFragment {
                     mAdapter.removeSelection(position);
                 }
 
-                // TODO maybe change: only recreate menu if count changes
-                if (menu != null) {
-                    menu.clear();
-                    if (checkedCount == 1) {
-                        createContextMenu(menu);
-                    } else {
-                        // download, move, copy, delete
-                        getActivity().getMenuInflater().inflate(R.menu.multiple_file_actions_menu, menu);
+                OCFile targetFile = null;
+                if (checkedCount > 0 && checkedCount <= MIN_FILES_FOR_MULTISELECT) {
+                    targetFile = (checkedCount == MIN_FILES_FOR_MULTISELECT)
+                        ? mFile : mAdapter.getCheckedItems().get(0);
+
+                    if (mContainerActivity.getStorageManager() != null) {
+                        FileMenuFilter mf = new FileMenuFilter(
+                            targetFile,
+                            mContainerActivity.getStorageManager().getAccount(),
+                            mContainerActivity,
+                            getActivity(),
+                            mAdapter.getCheckedItems().size() == MIN_FILES_FOR_MULTISELECT
+                        );
+                        mf.filter(menu);
                     }
+
+                } else {
+                    getListView().clearChoices();
+                    menu.clear();
+                    menu.close();
                 }
+
             }
 
             @Override
             public boolean onCreateActionMode(ActionMode mode, Menu menu) {
+
+                createContextActionBar(menu);
                 this.menu = menu;
 
                 //set gray color
@@ -560,46 +578,16 @@ public class OCFileListFragment extends ExtendedListFragment {
     }
 
     /**
-     * {@inheritDoc}
+     * Create the context bar with the file actions
+     * @param menu
      */
-    // TODO Tobi needed?
-    public void createContextMenu(Menu menu) {
+    public void createContextActionBar(Menu menu) {
         Bundle args = getArguments();
         boolean allowContextualActions =
                 (args == null) ? true : args.getBoolean(ARG_ALLOW_CONTEXTUAL_ACTIONS, true);
         if (allowContextualActions) {
             MenuInflater inflater = getActivity().getMenuInflater();
             inflater.inflate(R.menu.file_actions_menu, menu);
-            OCFile targetFile = null;
-            if (mAdapter.getCheckedItems().size() == 1){
-                targetFile = mAdapter.getCheckedItems().get(0);
-            }
-
-            if (mContainerActivity.getStorageManager() != null) {
-                FileMenuFilter mf = new FileMenuFilter(
-                    targetFile,
-                    mContainerActivity.getStorageManager().getAccount(),
-                    mContainerActivity,
-                    getActivity()
-                );
-                mf.filter(menu);
-            }
-
-            /// TODO break this direct dependency on FileDisplayActivity... if possible
-            MenuItem item = menu.findItem(R.id.action_open_file_with);
-            FileFragment frag = ((FileDisplayActivity)getActivity()).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);
-                }
-            }
-
-//            String.format(mContext.getString(R.string.subject_token),
-//                    getClient().getCredentials().getUsername(), file.getFileName()));
-
         }
     }