MenuCompat.java revision 1935ed3af7c6545bc38adfdc6026d87a3249222f
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 /** 27 * Interface for the full API. 28 */ 29 interface MenuVersionImpl { 30 /** @deprecated */ 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 /** @deprecated */ 39 @Override 40 public boolean setShowAsAction(MenuItem item, int actionEnum) { 41 return false; 42 } 43 } 44 45 /** 46 * Interface implementation for devices with at least v11 APIs. 47 */ 48 static class HoneycombMenuVersionImpl implements MenuVersionImpl { 49 /** @deprecated */ 50 @Override 51 public boolean setShowAsAction(MenuItem item, int actionEnum) { 52 MenuItemCompatHoneycomb.setShowAsAction(item, actionEnum); 53 return true; 54 } 55 } 56 57 /** 58 * Select the correct implementation to use for the current platform. 59 */ 60 static final MenuVersionImpl IMPL; 61 static { 62 if (android.os.Build.VERSION.SDK_INT >= 11) { 63 IMPL = new HoneycombMenuVersionImpl(); 64 } else { 65 IMPL = new BaseMenuVersionImpl(); 66 } 67 } 68 69 // ------------------------------------------------------------------- 70 71 /** 72 * Call {@link MenuItem#setShowAsAction(int) MenuItem.setShowAsAction()}. 73 * If running on a pre-{@android.os.Build.VERSION_CODES#HONEYCOMB} device, 74 * does nothing and returns false. Otherwise returns true. 75 * 76 * @deprecated Use {@link MenuItemCompat#setShowAsAction(MenuItem, int)} 77 */ 78 public static boolean setShowAsAction(MenuItem item, int actionEnum) { 79 return IMPL.setShowAsAction(item, actionEnum); 80 } 81} 82