1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.documentsui.picker;
18 
19 import static com.android.documentsui.base.DocumentInfo.getCursorString;
20 import static com.android.documentsui.base.State.ACTION_CREATE;
21 import static com.android.documentsui.base.State.ACTION_GET_CONTENT;
22 import static com.android.documentsui.base.State.ACTION_OPEN;
23 import static com.android.documentsui.base.State.ACTION_OPEN_TREE;
24 import static com.android.documentsui.base.State.ACTION_PICK_COPY_DESTINATION;
25 
26 import android.database.Cursor;
27 import android.provider.DocumentsContract.Document;
28 import android.view.KeyboardShortcutGroup;
29 import android.view.Menu;
30 import android.view.MenuItem;
31 
32 import com.android.documentsui.Model;
33 import com.android.documentsui.R;
34 import com.android.documentsui.base.Menus;
35 import com.android.documentsui.base.MimeTypes;
36 import com.android.documentsui.base.State;
37 import com.android.documentsui.queries.SearchViewManager;
38 
39 import java.util.List;
40 import java.util.function.IntFunction;
41 import java.util.function.IntSupplier;
42 
43 public final class MenuManager extends com.android.documentsui.MenuManager {
44 
45     private boolean mOnlyDirectory;
46 
MenuManager( SearchViewManager searchManager, State displayState, DirectoryDetails dirDetails, IntSupplier filesCountSupplier)47     public MenuManager(
48             SearchViewManager searchManager,
49             State displayState,
50             DirectoryDetails dirDetails,
51             IntSupplier filesCountSupplier) {
52         super(searchManager, displayState, dirDetails, filesCountSupplier);
53     }
54 
55     @Override
updateKeyboardShortcutsMenu( List<KeyboardShortcutGroup> data, IntFunction<String> stringSupplier)56     public void updateKeyboardShortcutsMenu(
57             List<KeyboardShortcutGroup> data, IntFunction<String> stringSupplier) {
58         // None as of yet.
59     }
60 
picking()61     private boolean picking() {
62         return mState.action == ACTION_CREATE
63                 || mState.action == ACTION_OPEN_TREE
64                 || mState.action == ACTION_PICK_COPY_DESTINATION;
65     }
66 
67     @Override
updateOptionMenu(Menu menu)68     public void updateOptionMenu(Menu menu) {
69         super.updateOptionMenu(menu);
70         if (picking()) {
71             // May already be hidden because the root
72             // doesn't support search.
73             mSearchManager.showMenu(null);
74 
75             // Show on toolbar because there are only two menu items while ACTION_OPEN_TREE.
76             menu.findItem(R.id.option_menu_sort).setShowAsAction(mState.action == ACTION_OPEN_TREE
77                     ? MenuItem.SHOW_AS_ACTION_ALWAYS : MenuItem.SHOW_AS_ACTION_NEVER);
78         }
79     }
80 
81     @Override
updateModel(Model model)82     public void updateModel(Model model) {
83         for (String id : model.getModelIds()) {
84             Cursor cursor = model.getItem(id);
85             String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
86             if (!MimeTypes.mimeMatches(Document.MIME_TYPE_DIR, docMimeType)) {
87                 mOnlyDirectory = false;
88                 return;
89             }
90         }
91         mOnlyDirectory = true;
92     }
93 
94     @Override
updateModePicker(MenuItem grid, MenuItem list)95     protected void updateModePicker(MenuItem grid, MenuItem list) {
96         // No display options in recent directories
97         if (picking() && mDirDetails.isInRecents()) {
98             Menus.setEnabledAndVisible(grid, false);
99             Menus.setEnabledAndVisible(list, false);
100         } else {
101             super.updateModePicker(grid, list);
102         }
103     }
104 
105     @Override
updateSelectAll(MenuItem selectAll)106     protected void updateSelectAll(MenuItem selectAll) {
107         boolean visible = mState.allowMultiple && !mOnlyDirectory;
108         Menus.setEnabledAndVisible(selectAll, visible);
109     }
110 
111     @Override
updateSelectAll(MenuItem selectAll, SelectionDetails selectionDetails)112     protected void updateSelectAll(MenuItem selectAll, SelectionDetails selectionDetails) {
113         final boolean visible = mState.allowMultiple
114                 && selectionDetails.size() < mFilesCountSupplier.getAsInt()
115                 && !mOnlyDirectory;
116         Menus.setEnabledAndVisible(selectAll, visible);
117     }
118 
119     @Override
120     protected void updateDeselectAll(MenuItem deselectAll, SelectionDetails selectionDetails) {
121         final boolean visible = mState.allowMultiple
122                 && selectionDetails.size() == mFilesCountSupplier.getAsInt()
123                 && !mOnlyDirectory;
124         Menus.setEnabledAndVisible(deselectAll, visible);
125     }
126 
127     @Override
128     protected void updateCreateDir(MenuItem createDir) {
129         createDir.setShowAsAction(picking()
130                 ? MenuItem.SHOW_AS_ACTION_ALWAYS : MenuItem.SHOW_AS_ACTION_NEVER);
131         Menus.setEnabledAndVisible(createDir, picking() && mDirDetails.canCreateDirectory());
132     }
133 
134     @Override
135     protected void updateSelect(MenuItem select, SelectionDetails selectionDetails) {
136         Menus.setEnabledAndVisible(select, (mState.action == ACTION_GET_CONTENT
137                 || mState.action == ACTION_OPEN)
138                 && selectionDetails.size() > 0);
139         select.setTitle(R.string.menu_select);
140     }
141 }
142