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