MenuCompat.java revision 27aea04b07c1fafa0f815aa4f80374a9e051b41c
1/*
2 * Copyright (C) 2011 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
17package android.support.v4.view;
18
19import android.view.MenuItem;
20
21/**
22 * Helper for accessing newer features in menus.
23 */
24public class MenuCompat {
25    /**
26     * Interface for the full API.
27     */
28    interface MenuVersionImpl {
29        public boolean setShowAsAction(MenuItem item, int actionEnum);
30    }
31
32    /**
33     * Interface implementation that doesn't use anything about v4 APIs.
34     */
35    static class BaseMenuVersionImpl implements MenuVersionImpl {
36        @Override
37        public boolean setShowAsAction(MenuItem item, int actionEnum) {
38            return false;
39        }
40    }
41
42    /**
43     * Interface implementation for devices with at least v11 APIs.
44     */
45    static class HoneycombMenuVersionImpl implements MenuVersionImpl {
46        @Override
47        public boolean setShowAsAction(MenuItem item, int actionEnum) {
48            MenuCompatHoneycomb.setShowAsAction(item, actionEnum);
49            return true;
50        }
51    }
52
53    /**
54     * Select the correct implementation to use for the current platform.
55     */
56    static final MenuVersionImpl IMPL;
57    static {
58        if (android.os.Build.VERSION.SDK_INT >= 11) {
59            IMPL = new HoneycombMenuVersionImpl();
60        } else {
61            IMPL = new BaseMenuVersionImpl();
62        }
63    }
64
65    // -------------------------------------------------------------------
66
67    /**
68     * Call {@link MenuItem#setShowAsAction(int) MenuItem.setShowAsAction()}.
69     * If running on a pre-{@android.os.Build.VERSION_CODES#HONEYCOMB} device,
70     * does nothing and returns false.  Otherwise returns true.
71     */
72    public static boolean setShowAsAction(MenuItem item, int actionEnum) {
73        return IMPL.setShowAsAction(item, actionEnum);
74    }
75}
76