ContextMenuBuilder.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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
17package com.android.internal.view.menu;
18
19import android.content.Context;
20import android.graphics.drawable.Drawable;
21import android.os.IBinder;
22import android.util.EventLog;
23import android.view.ContextMenu;
24import 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 #show(View, IBinder)} which will populate the menu
38 * with a view's context menu items and show the context menu.
39 */
40public class ContextMenuBuilder extends MenuBuilder implements ContextMenu {
41
42    public ContextMenuBuilder(Context context) {
43        super(context);
44    }
45
46    public ContextMenu setHeaderIcon(Drawable icon) {
47        return (ContextMenu) super.setHeaderIconInt(icon);
48    }
49
50    public ContextMenu setHeaderIcon(int iconRes) {
51        return (ContextMenu) super.setHeaderIconInt(iconRes);
52    }
53
54    public ContextMenu setHeaderTitle(CharSequence title) {
55        return (ContextMenu) super.setHeaderTitleInt(title);
56    }
57
58    public ContextMenu setHeaderTitle(int titleRes) {
59        return (ContextMenu) super.setHeaderTitleInt(titleRes);
60    }
61
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     */
77    public MenuDialogHelper show(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
96}
97