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 features in {@link android.view.Menu}
23 * introduced after API level 4 in a backwards compatible fashion.
24 */
25public class MenuCompat {
26
27    /**
28     * Interface for the full API.
29     */
30    interface MenuVersionImpl {
31        public boolean setShowAsAction(MenuItem item, int actionEnum);
32    }
33
34    /**
35     * Interface implementation that doesn't use anything about v4 APIs.
36     */
37    static class BaseMenuVersionImpl implements MenuVersionImpl {
38        @Override
39        public boolean setShowAsAction(MenuItem item, int actionEnum) {
40            return false;
41        }
42    }
43
44    /**
45     * Interface implementation for devices with at least v11 APIs.
46     */
47    static class HoneycombMenuVersionImpl implements MenuVersionImpl {
48        @Override
49        public boolean setShowAsAction(MenuItem item, int actionEnum) {
50            MenuItemCompatHoneycomb.setShowAsAction(item, actionEnum);
51            return true;
52        }
53    }
54
55    /**
56     * Select the correct implementation to use for the current platform.
57     */
58    static final MenuVersionImpl IMPL;
59    static {
60        if (android.os.Build.VERSION.SDK_INT >= 11) {
61            IMPL = new HoneycombMenuVersionImpl();
62        } else {
63            IMPL = new BaseMenuVersionImpl();
64        }
65    }
66
67    // -------------------------------------------------------------------
68
69    /**
70     * Call {@link MenuItem#setShowAsAction(int) MenuItem.setShowAsAction()}.
71     * If running on a pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} device,
72     * does nothing and returns false.  Otherwise returns true.
73     *
74     * @deprecated Use {@link MenuItemCompat#setShowAsAction(MenuItem, int)
75     *     MenuItemCompat.setShowAsAction(MenuItem, int)}
76
77     */
78    public static boolean setShowAsAction(MenuItem item, int actionEnum) {
79        return IMPL.setShowAsAction(item, actionEnum);
80    }
81}
82