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 | |
| 19 | import android.view.Menu; |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 20 | import android.view.MenuItem; |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 21 | |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame^] | 22 | import com.android.documentsui.base.RootInfo; |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 23 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 24 | public abstract class MenuManager { |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 25 | |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 26 | final State mState; |
| 27 | final SearchViewManager mSearchManager; |
| 28 | |
| 29 | public MenuManager(SearchViewManager searchManager, State displayState) { |
| 30 | mSearchManager = searchManager; |
| 31 | mState = displayState; |
| 32 | } |
| 33 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 34 | /** @See DirectoryFragment.SelectionModeListener#updateActionMenu */ |
| 35 | public void updateActionMenu(Menu menu, SelectionDetails selection) { |
| 36 | updateOpen(menu.findItem(R.id.menu_open), selection); |
| 37 | updateDelete(menu.findItem(R.id.menu_delete), selection); |
| 38 | updateShare(menu.findItem(R.id.menu_share), selection); |
| 39 | updateRename(menu.findItem(R.id.menu_rename), selection); |
| 40 | updateSelectAll(menu.findItem(R.id.menu_select_all), selection); |
| 41 | updateMoveTo(menu.findItem(R.id.menu_move_to), selection); |
| 42 | updateCopyTo(menu.findItem(R.id.menu_copy_to), selection); |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 43 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 44 | Menus.disableHiddenItems(menu); |
| 45 | } |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 46 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 47 | /** @See Activity#onPrepareOptionsMenu */ |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 48 | public void updateOptionMenu(Menu menu, DirectoryDetails directoryDetails) { |
| 49 | updateCreateDir(menu.findItem(R.id.menu_create_dir), directoryDetails); |
| 50 | updateSettings(menu.findItem(R.id.menu_settings), directoryDetails); |
| 51 | updateNewWindow(menu.findItem(R.id.menu_new_window), directoryDetails); |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 52 | updateModePicker(menu.findItem( |
| 53 | R.id.menu_grid), menu.findItem(R.id.menu_list), directoryDetails); |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 54 | // Sort menu item is managed by SortMenuManager |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 55 | updateAdvanced(menu.findItem(R.id.menu_advanced), directoryDetails); |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 56 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 57 | Menus.disableHiddenItems(menu); |
| 58 | } |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 59 | |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 60 | /** @See DirectoryFragment.onCreateContextMenu |
| 61 | * |
| 62 | * Called when user tries to generate a context menu anchored to a file. |
| 63 | * */ |
| 64 | public void updateContextMenuForFile( |
| 65 | Menu menu, |
| 66 | SelectionDetails selectionDetails, |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 67 | DirectoryDetails directoryDetails) { |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 68 | assert(selectionDetails != null); |
| 69 | |
| 70 | MenuItem cut = menu.findItem(R.id.menu_cut_to_clipboard); |
| 71 | MenuItem copy = menu.findItem(R.id.menu_copy_to_clipboard); |
| 72 | MenuItem pasteInto = menu.findItem(R.id.menu_paste_into_folder); |
| 73 | MenuItem delete = menu.findItem(R.id.menu_delete); |
| 74 | MenuItem rename = menu.findItem(R.id.menu_rename); |
| 75 | |
| 76 | copy.setEnabled(!selectionDetails.containsPartialFiles()); |
| 77 | cut.setEnabled( |
| 78 | !selectionDetails.containsPartialFiles() && selectionDetails.canDelete()); |
| 79 | updatePasteInto(pasteInto, selectionDetails); |
| 80 | updateRename(rename, selectionDetails); |
| 81 | updateDelete(delete, selectionDetails); |
| 82 | |
| 83 | updateContextMenu(menu, directoryDetails); |
| 84 | } |
| 85 | |
| 86 | /** @See DirectoryFragment.onCreateContextMenu |
| 87 | * |
| 88 | * Called when user tries to generate a context menu anchored to an empty pane. |
| 89 | * */ |
| 90 | public void updateContextMenuForContainer(Menu menu, DirectoryDetails directoryDetails) { |
| 91 | MenuItem cut = menu.findItem(R.id.menu_cut_to_clipboard); |
| 92 | MenuItem copy = menu.findItem(R.id.menu_copy_to_clipboard); |
| 93 | MenuItem pasteInto = menu.findItem(R.id.menu_paste_into_folder); |
| 94 | MenuItem delete = menu.findItem(R.id.menu_delete); |
| 95 | MenuItem rename = menu.findItem(R.id.menu_rename); |
| 96 | |
| 97 | cut.setEnabled(false); |
| 98 | copy.setEnabled(false); |
| 99 | pasteInto.setEnabled(false); |
| 100 | rename.setEnabled(false); |
| 101 | delete.setEnabled(false); |
| 102 | |
| 103 | updateContextMenu(menu, directoryDetails); |
| 104 | } |
| 105 | |
| 106 | private void updateContextMenu(Menu menu, DirectoryDetails directoryDetails) { |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 107 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 108 | MenuItem cut = menu.findItem(R.id.menu_cut_to_clipboard); |
| 109 | MenuItem copy = menu.findItem(R.id.menu_copy_to_clipboard); |
| 110 | MenuItem paste = menu.findItem(R.id.menu_paste_from_clipboard); |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 111 | MenuItem pasteInto = menu.findItem(R.id.menu_paste_into_folder); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 112 | MenuItem delete = menu.findItem(R.id.menu_delete); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 113 | MenuItem createDir = menu.findItem(R.id.menu_create_dir); |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 114 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 115 | updateCreateDir(createDir, directoryDetails); |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 116 | paste.setEnabled(directoryDetails.hasItemsToPaste()); |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 117 | |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 118 | //Cut, Copy and Delete should always be visible |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 119 | cut.setVisible(true); |
| 120 | copy.setVisible(true); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 121 | delete.setVisible(true); |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 122 | |
| 123 | // PasteInto should only show if it is enabled. If it's not enabled, Paste shows (regardless |
| 124 | // of whether it is enabled or not). |
| 125 | // Paste then hides itself whenever PasteInto is enabled/visible |
| 126 | pasteInto.setVisible(pasteInto.isEnabled()); |
| 127 | paste.setVisible(!pasteInto.isVisible()); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 128 | } |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 129 | |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 130 | public void updateRootContextMenu(Menu menu, RootInfo root) { |
| 131 | MenuItem settings = menu.findItem(R.id.menu_settings); |
| 132 | MenuItem eject = menu.findItem(R.id.menu_eject_root); |
| 133 | |
| 134 | updateSettings(settings, root); |
| 135 | updateEject(eject, root); |
| 136 | } |
| 137 | |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 138 | void updateModePicker(MenuItem grid, MenuItem list, DirectoryDetails directoryDetails) { |
| 139 | grid.setVisible(mState.derivedMode != State.MODE_GRID); |
| 140 | list.setVisible(mState.derivedMode != State.MODE_LIST); |
| 141 | } |
| 142 | |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 143 | void updateAdvanced(MenuItem advanced, DirectoryDetails directoryDetails) { |
| 144 | advanced.setVisible(mState.showAdvancedOption); |
| 145 | advanced.setTitle(mState.showAdvancedOption && mState.showAdvanced |
| 146 | ? R.string.menu_advanced_hide : R.string.menu_advanced_show); |
| 147 | } |
| 148 | |
| 149 | void updateSettings(MenuItem settings, DirectoryDetails directoryDetails) { |
| 150 | settings.setVisible(false); |
| 151 | } |
| 152 | |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 153 | void updateSettings(MenuItem settings, RootInfo root) { |
| 154 | settings.setVisible(false); |
| 155 | } |
| 156 | |
| 157 | void updateEject(MenuItem eject, RootInfo root) { |
| 158 | eject.setVisible(false); |
| 159 | } |
| 160 | |
Ben Lin | 2567991 | 2016-06-17 11:48:57 -0700 | [diff] [blame] | 161 | void updateNewWindow(MenuItem newWindow, DirectoryDetails directoryDetails) { |
| 162 | newWindow.setVisible(false); |
| 163 | } |
| 164 | |
| 165 | void updateOpen(MenuItem open, SelectionDetails selectionDetails) { |
| 166 | open.setVisible(false); |
| 167 | } |
| 168 | |
| 169 | void updateShare(MenuItem share, SelectionDetails selectionDetails) { |
| 170 | share.setVisible(false); |
| 171 | } |
| 172 | |
| 173 | void updateDelete(MenuItem delete, SelectionDetails selectionDetails) { |
| 174 | delete.setVisible(false); |
| 175 | } |
| 176 | |
| 177 | void updateRename(MenuItem rename, SelectionDetails selectionDetails) { |
| 178 | rename.setVisible(false); |
| 179 | } |
| 180 | |
| 181 | void updateMoveTo(MenuItem moveTo, SelectionDetails selectionDetails) { |
| 182 | moveTo.setVisible(false); |
| 183 | } |
| 184 | |
| 185 | void updateCopyTo(MenuItem copyTo, SelectionDetails selectionDetails) { |
| 186 | copyTo.setVisible(false); |
| 187 | } |
| 188 | |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 189 | void updatePasteInto(MenuItem pasteInto, SelectionDetails selectionDetails) { |
| 190 | pasteInto.setEnabled(false); |
| 191 | } |
| 192 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 193 | abstract void updateSelectAll(MenuItem selectAll, SelectionDetails selectionDetails); |
| 194 | abstract void updateCreateDir(MenuItem createDir, DirectoryDetails directoryDetails); |
Ben Lin | 7c35b03 | 2016-05-31 13:24:01 -0700 | [diff] [blame] | 195 | |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 196 | /** |
| 197 | * Access to meta data about the selection. |
| 198 | */ |
| 199 | public interface SelectionDetails { |
| 200 | boolean containsDirectories(); |
| 201 | |
| 202 | boolean containsPartialFiles(); |
| 203 | |
| 204 | // TODO: Update these to express characteristics instead of answering concrete questions, |
| 205 | // since the answer to those questions is (or can be) activity specific. |
| 206 | boolean canDelete(); |
| 207 | |
| 208 | boolean canRename(); |
Ben Lin | b2dfa12 | 2016-08-11 17:42:57 -0700 | [diff] [blame] | 209 | |
| 210 | boolean canPasteInto(); |
Ben Lin | ebf2a17 | 2016-06-03 13:46:52 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | public static class DirectoryDetails { |
| 214 | private final BaseActivity mActivity; |
| 215 | |
| 216 | public DirectoryDetails(BaseActivity activity) { |
| 217 | mActivity = activity; |
| 218 | } |
| 219 | |
| 220 | public boolean shouldShowFancyFeatures() { |
| 221 | return Shared.shouldShowFancyFeatures(mActivity); |
| 222 | } |
| 223 | |
| 224 | public boolean hasRootSettings() { |
| 225 | return mActivity.getCurrentRoot().hasSettings(); |
| 226 | } |
| 227 | |
| 228 | public boolean hasItemsToPaste() { |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | public boolean isInRecents() { |
| 233 | return mActivity.getCurrentDirectory() == null; |
| 234 | } |
| 235 | |
| 236 | public boolean canCreateDirectory() { |
| 237 | return mActivity.canCreateDirectory(); |
| 238 | } |
| 239 | } |
| 240 | } |