1 /*
2  * Copyright (C) 2015 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.base;
18 
19 import android.view.Menu;
20 import android.view.MenuItem;
21 
22 public final class Menus {
23 
Menus()24     private Menus() {}
25 
26     /**
27      * Disables hidden menu items so that they are not invokable via command shortcuts
28      */
disableHiddenItems(Menu menu, MenuItem... exclusions)29     public static void disableHiddenItems(Menu menu, MenuItem... exclusions) {
30         for (int i = 0; i < menu.size(); i++) {
31             MenuItem item = menu.getItem(i);
32             if (item.isVisible()) {
33                 item.setEnabled(true);
34                 continue;
35             }
36             if (contains(exclusions, item)) {
37                 continue;
38             }
39             item.setEnabled(false);
40         }
41     }
42 
43     /** Set enabled/disabled state of a menuItem, and updates its visibility. */
setEnabledAndVisible(MenuItem item, boolean enabled)44     public static void setEnabledAndVisible(MenuItem item, boolean enabled) {
45         item.setEnabled(enabled);
46         item.setVisible(enabled);
47     }
48 
contains(MenuItem[] exclusions, MenuItem item)49     private static boolean contains(MenuItem[] exclusions, MenuItem item) {
50         for (int x = 0; x < exclusions.length; x++) {
51             if (exclusions[x] == item) {
52                 return true;
53             }
54         }
55         return false;
56     }
57 }
58