1 /* 2 * Copyright (C) 2006 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.internal.view.menu; 18 19 import android.content.Context; 20 import android.graphics.drawable.Drawable; 21 import android.os.IBinder; 22 import android.util.EventLog; 23 import android.view.ContextMenu; 24 import android.view.View; 25 26 /** 27 * Implementation of the {@link android.view.ContextMenu} interface. 28 * <p> 29 * Most clients of the menu framework will never need to touch this 30 * class. However, if the client has a window that 31 * is not a content view of a Dialog or Activity (for example, the 32 * view was added directly to the window manager) and needs to show 33 * context menus, it will use this class. 34 * <p> 35 * To use this class, instantiate it via {@link #ContextMenuBuilder(Context)}, 36 * and optionally populate it with any of your custom items. Finally, 37 * call {@link #showDialog(View, IBinder)} which will populate the menu 38 * with a view's context menu items and show the context menu. 39 */ 40 public class ContextMenuBuilder extends MenuBuilder implements ContextMenu { 41 ContextMenuBuilder(Context context)42 public ContextMenuBuilder(Context context) { 43 super(context); 44 } 45 setHeaderIcon(Drawable icon)46 public ContextMenu setHeaderIcon(Drawable icon) { 47 return (ContextMenu) super.setHeaderIconInt(icon); 48 } 49 setHeaderIcon(int iconRes)50 public ContextMenu setHeaderIcon(int iconRes) { 51 return (ContextMenu) super.setHeaderIconInt(iconRes); 52 } 53 setHeaderTitle(CharSequence title)54 public ContextMenu setHeaderTitle(CharSequence title) { 55 return (ContextMenu) super.setHeaderTitleInt(title); 56 } 57 setHeaderTitle(int titleRes)58 public ContextMenu setHeaderTitle(int titleRes) { 59 return (ContextMenu) super.setHeaderTitleInt(titleRes); 60 } 61 setHeaderView(View view)62 public ContextMenu setHeaderView(View view) { 63 return (ContextMenu) super.setHeaderViewInt(view); 64 } 65 66 /** 67 * Shows this context menu, allowing the optional original view (and its 68 * ancestors) to add items. 69 * 70 * @param originalView Optional, the original view that triggered the 71 * context menu. 72 * @param token Optional, the window token that should be set on the context 73 * menu's window. 74 * @return If the context menu was shown, the {@link MenuDialogHelper} for 75 * dismissing it. Otherwise, null. 76 */ showDialog(View originalView, IBinder token)77 public MenuDialogHelper showDialog(View originalView, IBinder token) { 78 if (originalView != null) { 79 // Let relevant views and their populate context listeners populate 80 // the context menu 81 originalView.createContextMenu(this); 82 } 83 84 if (getVisibleItems().size() > 0) { 85 EventLog.writeEvent(50001, 1); 86 87 MenuDialogHelper helper = new MenuDialogHelper(this); 88 helper.show(token); 89 90 return helper; 91 } 92 93 return null; 94 } 95 showPopup(Context context, View originalView, float x, float y)96 public MenuPopupHelper showPopup(Context context, View originalView, float x, float y) { 97 if (originalView != null) { 98 // Let relevant views and their populate context listeners populate 99 // the context menu 100 originalView.createContextMenu(this); 101 } 102 103 if (getVisibleItems().size() > 0) { 104 EventLog.writeEvent(50001, 1); 105 106 int location[] = new int[2]; 107 originalView.getLocationOnScreen(location); 108 109 final MenuPopupHelper helper = new MenuPopupHelper( 110 context, 111 this, 112 originalView, 113 false /* overflowOnly */, 114 com.android.internal.R.attr.contextPopupMenuStyle); 115 helper.show(Math.round(x), Math.round(y)); 116 return helper; 117 } 118 119 return null; 120 } 121 } 122