Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 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; |
| 18 | |
Garfield Tan | 239ab97 | 2016-09-16 13:33:48 -0700 | [diff] [blame] | 19 | import android.app.Fragment; |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 20 | import android.view.Menu; |
Garfield Tan | 239ab97 | 2016-09-16 13:33:48 -0700 | [diff] [blame] | 21 | import android.view.MenuInflater; |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 22 | import android.view.MenuItem; |
Garfield Tan | 239ab97 | 2016-09-16 13:33:48 -0700 | [diff] [blame] | 23 | import android.view.View; |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 24 | |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 25 | import com.android.documentsui.base.Menus; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 26 | import com.android.documentsui.base.RootInfo; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 27 | import com.android.documentsui.base.Shared; |
| 28 | import com.android.documentsui.base.State; |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 29 | import com.android.documentsui.dirlist.DirectoryFragment; |
Garfield Tan | 239ab97 | 2016-09-16 13:33:48 -0700 | [diff] [blame] | 30 | import com.android.internal.annotations.VisibleForTesting; |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 31 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 32 | public abstract class MenuManager { |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 33 | |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 34 | final State mState; |
| 35 | final SearchViewManager mSearchManager; |
| 36 | |
| 37 | public MenuManager(SearchViewManager searchManager, State displayState) { |
| 38 | mSearchManager = searchManager; |
| 39 | mState = displayState; |
| 40 | } |
| 41 | |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 42 | /** @see ActionModeController */ |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 43 | public void updateActionMenu(Menu menu, SelectionDetails selection) { |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 44 | updateOpenInActionMode(menu.findItem(R.id.menu_open), selection); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 45 | updateDelete(menu.findItem(R.id.menu_delete), selection); |
| 46 | updateShare(menu.findItem(R.id.menu_share), selection); |
| 47 | updateRename(menu.findItem(R.id.menu_rename), selection); |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 48 | updateSelectAll(menu.findItem(R.id.menu_select_all)); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 49 | updateMoveTo(menu.findItem(R.id.menu_move_to), selection); |
| 50 | updateCopyTo(menu.findItem(R.id.menu_copy_to), selection); |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 51 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 52 | Menus.disableHiddenItems(menu); |
| 53 | } |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 54 | |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 55 | /** @see BaseActivity#onPrepareOptionsMenu */ |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 56 | public void updateOptionMenu(Menu menu, DirectoryDetails directoryDetails) { |
| 57 | updateCreateDir(menu.findItem(R.id.menu_create_dir), directoryDetails); |
| 58 | updateSettings(menu.findItem(R.id.menu_settings), directoryDetails); |
| 59 | updateNewWindow(menu.findItem(R.id.menu_new_window), directoryDetails); |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 60 | updateModePicker(menu.findItem( |
| 61 | R.id.menu_grid), menu.findItem(R.id.menu_list), directoryDetails); |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 62 | // Sort menu item is managed by SortMenuManager |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 63 | updateAdvanced(menu.findItem(R.id.menu_advanced), directoryDetails); |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 64 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 65 | Menus.disableHiddenItems(menu); |
| 66 | } |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 67 | |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 68 | /** |
Garfield Tan | 239ab97 | 2016-09-16 13:33:48 -0700 | [diff] [blame] | 69 | * Called when we needs {@link MenuManager} to ask Android to show context menu for us. |
| 70 | * {@link MenuManager} can choose to defeat this request. |
| 71 | * |
| 72 | * {@link #inflateContextMenuForDocs} and {@link #inflateContextMenuForContainer} are called |
| 73 | * afterwards when Android asks us to provide the content of context menus, so they're not |
| 74 | * correct locations to suppress context menus. |
| 75 | */ |
| 76 | public void showContextMenu(Fragment f, View v, float x, float y) { |
| 77 | // Pickers don't have any context menu at this moment. |
| 78 | } |
| 79 | |
| 80 | public void inflateContextMenuForContainer( |
| 81 | Menu menu, MenuInflater inflater, DirectoryDetails directoryDetails) { |
| 82 | throw new UnsupportedOperationException("Pickers don't allow context menu."); |
| 83 | } |
| 84 | |
| 85 | public void inflateContextMenuForDocs( |
| 86 | Menu menu, MenuInflater inflater, SelectionDetails selectionDetails) { |
| 87 | throw new UnsupportedOperationException("Pickers don't allow context menu."); |
| 88 | } |
| 89 | |
| 90 | /** |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 91 | * @see DirectoryFragment#onCreateContextMenu |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 92 | * |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 93 | * Called when user tries to generate a context menu anchored to a file when the selection |
| 94 | * doesn't contain any folder. |
| 95 | * |
| 96 | * @param selectionDetails |
| 97 | * containsFiles may return false because this may be called when user right clicks on an |
| 98 | * unselectable item in pickers |
| 99 | */ |
Garfield Tan | 239ab97 | 2016-09-16 13:33:48 -0700 | [diff] [blame] | 100 | @VisibleForTesting |
| 101 | void updateContextMenuForFiles(Menu menu, SelectionDetails selectionDetails) { |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 102 | assert(selectionDetails != null); |
| 103 | |
| 104 | MenuItem share = menu.findItem(R.id.menu_share); |
| 105 | MenuItem open = menu.findItem(R.id.menu_open); |
| 106 | MenuItem openWith = menu.findItem(R.id.menu_open_with); |
| 107 | MenuItem rename = menu.findItem(R.id.menu_rename); |
| 108 | |
| 109 | updateShare(share, selectionDetails); |
| 110 | updateOpenInContextMenu(open, selectionDetails); |
| 111 | updateOpenWith(openWith, selectionDetails); |
| 112 | updateRename(rename, selectionDetails); |
| 113 | |
| 114 | updateContextMenu(menu, selectionDetails); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @see DirectoryFragment#onCreateContextMenu |
| 119 | * |
| 120 | * Called when user tries to generate a context menu anchored to a folder when the selection |
| 121 | * doesn't contain any file. |
| 122 | * |
| 123 | * @param selectionDetails |
| 124 | * containDirectories may return false because this may be called when user right clicks on |
| 125 | * an unselectable item in pickers |
| 126 | */ |
Garfield Tan | 239ab97 | 2016-09-16 13:33:48 -0700 | [diff] [blame] | 127 | @VisibleForTesting |
| 128 | void updateContextMenuForDirs(Menu menu, SelectionDetails selectionDetails) { |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 129 | assert(selectionDetails != null); |
| 130 | |
| 131 | MenuItem openInNewWindow = menu.findItem(R.id.menu_open_in_new_window); |
| 132 | MenuItem rename = menu.findItem(R.id.menu_rename); |
| 133 | MenuItem pasteInto = menu.findItem(R.id.menu_paste_into_folder); |
| 134 | |
| 135 | updateOpenInNewWindow(openInNewWindow, selectionDetails); |
| 136 | updateRename(rename, selectionDetails); |
| 137 | updatePasteInto(pasteInto, selectionDetails); |
| 138 | |
| 139 | updateContextMenu(menu, selectionDetails); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @see DirectoryFragment#onCreateContextMenu |
| 144 | * |
| 145 | * Update shared context menu items of both files and folders context menus. |
| 146 | */ |
Garfield Tan | 239ab97 | 2016-09-16 13:33:48 -0700 | [diff] [blame] | 147 | @VisibleForTesting |
| 148 | void updateContextMenu(Menu menu, SelectionDetails selectionDetails) { |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 149 | assert(selectionDetails != null); |
| 150 | |
| 151 | MenuItem cut = menu.findItem(R.id.menu_cut_to_clipboard); |
| 152 | MenuItem copy = menu.findItem(R.id.menu_copy_to_clipboard); |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 153 | MenuItem delete = menu.findItem(R.id.menu_delete); |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 154 | |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 155 | final boolean canCopy = |
| 156 | selectionDetails.size() > 0 && !selectionDetails.containsPartialFiles(); |
| 157 | final boolean canDelete = selectionDetails.canDelete(); |
| 158 | cut.setEnabled(canCopy && canDelete); |
| 159 | copy.setEnabled(canCopy); |
| 160 | delete.setEnabled(canDelete); |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 163 | /** |
| 164 | * @see DirectoryFragment#onCreateContextMenu |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 165 | * |
| 166 | * Called when user tries to generate a context menu anchored to an empty pane. |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 167 | */ |
Garfield Tan | 239ab97 | 2016-09-16 13:33:48 -0700 | [diff] [blame] | 168 | @VisibleForTesting |
| 169 | void updateContextMenuForContainer(Menu menu, DirectoryDetails directoryDetails) { |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 170 | MenuItem paste = menu.findItem(R.id.menu_paste_from_clipboard); |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 171 | MenuItem selectAll = menu.findItem(R.id.menu_select_all); |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 172 | |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 173 | paste.setEnabled(directoryDetails.hasItemsToPaste() && directoryDetails.canCreateDoc()); |
| 174 | updateSelectAll(selectAll); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 175 | } |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 176 | |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 177 | /** |
| 178 | * @see RootsFragment#onCreateContextMenu |
| 179 | */ |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 180 | public void updateRootContextMenu(Menu menu, RootInfo root) { |
| 181 | MenuItem settings = menu.findItem(R.id.menu_settings); |
| 182 | MenuItem eject = menu.findItem(R.id.menu_eject_root); |
| 183 | |
| 184 | updateSettings(settings, root); |
| 185 | updateEject(eject, root); |
| 186 | } |
| 187 | |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 188 | void updateModePicker(MenuItem grid, MenuItem list, DirectoryDetails directoryDetails) { |
| 189 | grid.setVisible(mState.derivedMode != State.MODE_GRID); |
| 190 | list.setVisible(mState.derivedMode != State.MODE_LIST); |
| 191 | } |
| 192 | |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 193 | void updateAdvanced(MenuItem advanced, DirectoryDetails directoryDetails) { |
| 194 | advanced.setVisible(mState.showAdvancedOption); |
| 195 | advanced.setTitle(mState.showAdvancedOption && mState.showAdvanced |
| 196 | ? R.string.menu_advanced_hide : R.string.menu_advanced_show); |
| 197 | } |
| 198 | |
| 199 | void updateSettings(MenuItem settings, DirectoryDetails directoryDetails) { |
| 200 | settings.setVisible(false); |
| 201 | } |
| 202 | |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 203 | void updateSettings(MenuItem settings, RootInfo root) { |
| 204 | settings.setVisible(false); |
| 205 | } |
| 206 | |
| 207 | void updateEject(MenuItem eject, RootInfo root) { |
| 208 | eject.setVisible(false); |
| 209 | } |
| 210 | |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 211 | void updateNewWindow(MenuItem newWindow, DirectoryDetails directoryDetails) { |
| 212 | newWindow.setVisible(false); |
| 213 | } |
| 214 | |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 215 | void updateOpenInActionMode(MenuItem open, SelectionDetails selectionDetails) { |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 216 | open.setVisible(false); |
| 217 | } |
| 218 | |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 219 | void updateOpenWith(MenuItem openWith, SelectionDetails selectionDetails) { |
| 220 | openWith.setVisible(false); |
| 221 | } |
| 222 | |
| 223 | void updateOpenInNewWindow(MenuItem openInNewWindow, SelectionDetails selectionDetails) { |
| 224 | openInNewWindow.setVisible(false); |
| 225 | } |
| 226 | |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 227 | void updateShare(MenuItem share, SelectionDetails selectionDetails) { |
| 228 | share.setVisible(false); |
| 229 | } |
| 230 | |
| 231 | void updateDelete(MenuItem delete, SelectionDetails selectionDetails) { |
| 232 | delete.setVisible(false); |
| 233 | } |
| 234 | |
| 235 | void updateRename(MenuItem rename, SelectionDetails selectionDetails) { |
| 236 | rename.setVisible(false); |
| 237 | } |
| 238 | |
| 239 | void updateMoveTo(MenuItem moveTo, SelectionDetails selectionDetails) { |
| 240 | moveTo.setVisible(false); |
| 241 | } |
| 242 | |
| 243 | void updateCopyTo(MenuItem copyTo, SelectionDetails selectionDetails) { |
| 244 | copyTo.setVisible(false); |
| 245 | } |
| 246 | |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 247 | void updatePasteInto(MenuItem pasteInto, SelectionDetails selectionDetails) { |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 248 | pasteInto.setVisible(false); |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 251 | abstract void updateOpenInContextMenu(MenuItem open, SelectionDetails selectionDetails); |
| 252 | abstract void updateSelectAll(MenuItem selectAll); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 253 | abstract void updateCreateDir(MenuItem createDir, DirectoryDetails directoryDetails); |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 254 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Access to meta data about the selection. |
| 257 | */ |
| 258 | public interface SelectionDetails { |
| 259 | boolean containsDirectories(); |
| 260 | |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 261 | boolean containsFiles(); |
| 262 | |
| 263 | int size(); |
| 264 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 265 | boolean containsPartialFiles(); |
| 266 | |
| 267 | // TODO: Update these to express characteristics instead of answering concrete questions, |
| 268 | // since the answer to those questions is (or can be) activity specific. |
| 269 | boolean canDelete(); |
| 270 | |
| 271 | boolean canRename(); |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 272 | |
| 273 | boolean canPasteInto(); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | public static class DirectoryDetails { |
| 277 | private final BaseActivity mActivity; |
| 278 | |
| 279 | public DirectoryDetails(BaseActivity activity) { |
| 280 | mActivity = activity; |
| 281 | } |
| 282 | |
| 283 | public boolean shouldShowFancyFeatures() { |
| 284 | return Shared.shouldShowFancyFeatures(mActivity); |
| 285 | } |
| 286 | |
| 287 | public boolean hasRootSettings() { |
| 288 | return mActivity.getCurrentRoot().hasSettings(); |
| 289 | } |
| 290 | |
| 291 | public boolean hasItemsToPaste() { |
| 292 | return false; |
| 293 | } |
| 294 | |
Garfield Tan | 06c4b11 | 2016-09-14 11:05:58 -0700 | [diff] [blame] | 295 | public boolean canCreateDoc() { |
| 296 | return isInRecents() ? false : mActivity.getCurrentDirectory().isCreateSupported(); |
| 297 | } |
| 298 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 299 | public boolean isInRecents() { |
| 300 | return mActivity.getCurrentDirectory() == null; |
| 301 | } |
| 302 | |
| 303 | public boolean canCreateDirectory() { |
| 304 | return mActivity.canCreateDirectory(); |
| 305 | } |
| 306 | } |
| 307 | } |