11935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov/*
21935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * Copyright (C) 2011 The Android Open Source Project
31935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov *
41935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * Licensed under the Apache License, Version 2.0 (the "License");
51935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * you may not use this file except in compliance with the License.
61935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * You may obtain a copy of the License at
71935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov *
81935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov *      http://www.apache.org/licenses/LICENSE-2.0
91935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov *
101935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * Unless required by applicable law or agreed to in writing, software
111935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * distributed under the License is distributed on an "AS IS" BASIS,
121935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * See the License for the specific language governing permissions and
141935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov * limitations under the License.
151935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov */
161935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
171935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganovpackage android.support.v4.view;
181935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
191935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganovimport android.view.MenuItem;
201935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganovimport android.view.View;
211935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
221935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov/**
230574ca37da4619afe4e26753f5a1b4de314b6565Svetoslav Ganov * Helper for accessing features in {@link android.view.MenuItem}
240574ca37da4619afe4e26753f5a1b4de314b6565Svetoslav Ganov * introduced after API level 4 in a backwards compatible fashion.
251935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov */
261935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganovpublic class MenuItemCompat {
271935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
281935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    /**
291935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * Never show this item as a button in an Action Bar.
301935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     */
311935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    public static final int SHOW_AS_ACTION_NEVER = 0;
321935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
331935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    /**
341935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * Show this item as a button in an Action Bar if the system
351935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * decides there is room for it.
361935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     */
371935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    public static final int SHOW_AS_ACTION_IF_ROOM = 1;
381935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
391935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    /**
401935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * Always show this item as a button in an Action Bar. Use sparingly!
411935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * If too many items are set to always show in the Action Bar it can
421935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * crowd the Action Bar and degrade the user experience on devices with
431935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * smaller screens. A good rule of thumb is to have no more than 2
441935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * items set to always show at a time.
451935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     */
461935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    public static final int SHOW_AS_ACTION_ALWAYS = 2;
471935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
481935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    /**
491935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * When this item is in the action bar, always show it with a
501935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * text label even if it also has an icon specified.
511935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     */
521935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    public static final int SHOW_AS_ACTION_WITH_TEXT = 4;
531935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
541935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    /**
551935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * This item's action view collapses to a normal menu item.
561935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * When expanded, the action view temporarily takes over
571935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * a larger segment of its container.
581935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     */
591935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    public static final int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW = 8;
601935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
611935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    /**
621935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * Interface for the full API.
631935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     */
641935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    interface MenuVersionImpl {
651935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        public boolean setShowAsAction(MenuItem item, int actionEnum);
661935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        public MenuItem setActionView(MenuItem item, View view);
671935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    }
681935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
691935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    /**
701935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * Interface implementation that doesn't use anything about v4 APIs.
711935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     */
721935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    static class BaseMenuVersionImpl implements MenuVersionImpl {
731935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        @Override
741935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        public boolean setShowAsAction(MenuItem item, int actionEnum) {
751935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov            return false;
761935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        }
771935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
781935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        @Override
791935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        public MenuItem setActionView(MenuItem item, View view) {
801935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov            return item;
811935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        }
821935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    }
831935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
841935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    /**
851935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * Interface implementation for devices with at least v11 APIs.
861935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     */
871935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    static class HoneycombMenuVersionImpl implements MenuVersionImpl {
881935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        @Override
891935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        public boolean setShowAsAction(MenuItem item, int actionEnum) {
901935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov            MenuItemCompatHoneycomb.setShowAsAction(item, actionEnum);
911935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov            return true;
921935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        }
931935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        @Override
941935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        public MenuItem setActionView(MenuItem item, View view) {
951935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov            return MenuItemCompatHoneycomb.setActionView(item, view);
961935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        }
971935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    }
981935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
991935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    /**
1001935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * Select the correct implementation to use for the current platform.
1011935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     */
1021935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    static final MenuVersionImpl IMPL;
1031935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    static {
1041935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        if (android.os.Build.VERSION.SDK_INT >= 11) {
1051935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov            IMPL = new HoneycombMenuVersionImpl();
1061935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        } else {
1071935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov            IMPL = new BaseMenuVersionImpl();
1081935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        }
1091935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    }
1101935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
1111935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    // -------------------------------------------------------------------
1121935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
1131935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    /**
1141935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * Call {@link MenuItem#setShowAsAction(int) MenuItem.setShowAsAction()}.
1150574ca37da4619afe4e26753f5a1b4de314b6565Svetoslav Ganov     * If running on a pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} device,
1161935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * does nothing and returns false.  Otherwise returns true.
1171935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     */
1181935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    public static boolean setShowAsAction(MenuItem item, int actionEnum) {
1191935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        return IMPL.setShowAsAction(item, actionEnum);
1201935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    }
1211935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov
1221935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    /**
1231935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * Set an action view for this menu item. An action view will be displayed in place
1241935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * of an automatically generated menu item element in the UI when this item is shown
1251935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * as an action within a parent.
1261935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     *
1271935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * @param view View to use for presenting this item to the user.
1281935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * @return This Item so additional setters can be called.
1291935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     *
1301935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     * @see #setShowAsAction(MenuItem, int)
1311935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov     */
1321935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    public static MenuItem setActionView(MenuItem item, View view) {
1331935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov        return IMPL.setActionView(item, view);
1341935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov    }
1351935ed3af7c6545bc38adfdc6026d87a3249222fSvetoslav Ganov}
136