summaryrefslogtreecommitdiffstats
path: root/tests/FrameworkTest/src/com/android/frameworktest/menus/MenuScenario.java
blob: 4df9b1bac905c6b5203d8f83c35d49c553607379 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.frameworktest.menus;

import com.android.frameworktest.util.ListScenario;
import com.android.internal.view.menu.MenuBuilder;
import com.android.internal.view.menu.MenuBuilder.MenuAdapter;

import android.app.Activity;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

/**
 * Utility base class for creating various Menu scenarios. Configurable by the
 * number of menu items. Used @link {@link ListScenario} as a reference.
 */
public class MenuScenario extends Activity implements MenuItem.OnMenuItemClickListener {
    private Params mParams = new Params();
    private Menu mMenu;
    private MenuItem[] mItems;
    private boolean[] mWasItemClicked;
    private MenuAdapter[] mMenuAdapters = new MenuAdapter[MenuBuilder.NUM_TYPES];
    
    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        dispatchInitParams();
    }

    private void dispatchInitParams() {
        onInitParams(mParams);
        onParamsChanged();
    }
    
    public void setParams(Params params) {
        mParams = params;
        onParamsChanged();
    }
    
    public void onParamsChanged() {
        mItems = new MenuItem[mParams.numItems];
        mWasItemClicked = new boolean[mParams.numItems];
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Safe to hold on to
        mMenu = menu;
        
        if (!mParams.shouldShowMenu) return false;
        
        MenuItem item;
        for (int i = 0; i < mParams.numItems; i++) {
            if ((item = onAddMenuItem(menu, i)) == null) {
                // Add a default item for this position if the subclasses
                // haven't
                CharSequence givenTitle = mParams.itemTitles.get(i);
                item = menu.add(0, 0, 0, (givenTitle != null) ? givenTitle : ("Item " + i));
            }
    
            if (item != null) {
                mItems[i] = item;
                
                if (mParams.listenForClicks) {
                    item.setOnMenuItemClickListener(this);
                }
            }
                
        }
        
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // Safe to hold on to
        mMenu = menu;

        return mParams.shouldShowMenu;
    }

    /**
     * Override this to add an item to the menu.
     * 
     * @param itemPosition The position of the item to add (only for your
     *            reference).
     * @return The item that was added to the menu, or null if nothing was
     *         added.
     */
    protected MenuItem onAddMenuItem(Menu menu, int itemPosition) {
        return null;
    }
    
    /**
     * Override this to set the parameters for the scenario. Call through to super first.
     * 
     * @param params
     */
    protected void onInitParams(Params params) {
    }
    
    public Menu getMenu() {
        return mMenu;
    }
    
    public boolean onMenuItemClick(MenuItem item) {
        final int position = findItemPosition(item);
        if (position < 0) return false;
        
        mWasItemClicked[position] = true;
        
        return true;
    }

    public boolean wasItemClicked(int position) {
        return mWasItemClicked[position];
    }

    /**
     * Finds the position for a given Item.
     * 
     * @param item The item to find.
     * @return The position, or -1 if not found.
     */
    public int findItemPosition(MenuItem item) {
        // Could create reverse mapping, but optimizations aren't important (yet :P)
        for (int i = 0; i < mParams.numItems; i++) {
            if (mItems[i] == item) return i;
        }
        
        return -1;
    }
    
    /**
     * @see MenuBuilder#getMenuAdapter(int)
     */
    public MenuAdapter getMenuAdapter(int menuType) {
        if (mMenuAdapters[menuType] == null) {
            mMenuAdapters[menuType] = ((MenuBuilder) mMenu).getMenuAdapter(menuType);
        }
        
        return mMenuAdapters[menuType];
    }

    /**
     * Gets a menu view. Call this after you're sure it has been shown,
     * otherwise it may not have the proper layout_* attributes set.
     * 
     * @param menuType The type of menu.
     * @return The MenuView for that type.
     */
    public View getMenuView(int menuType) {
        return ((MenuBuilder) mMenu).getMenuView(menuType, null);
    }
    
    /**
     * Gets the menu item view for a given position.
     * 
     * @param menuType The type of menu.
     * @param position The position of the item.
     * @return The menu item view for the given item in the given menu type.
     */
    public View getItemView(int menuType, int position) {
        return getMenuAdapter(menuType).getView(position, null, null);
    }
    
    public static class Params {
        // Using as data structure, so no m prefix
        private boolean shouldShowMenu = true;
        private int numItems = 10;
        private boolean listenForClicks = true;
        private SparseArray<CharSequence> itemTitles = new SparseArray<CharSequence>();

        public Params setShouldShowMenu(boolean shouldShowMenu) {
            this.shouldShowMenu = shouldShowMenu;
            return this;
        }
        
        public Params setNumItems(int numItems) {
            this.numItems = numItems;
            return this;
        }
        
        public Params setListenForClicks(boolean listenForClicks) {
            this.listenForClicks = listenForClicks;
            return this;
        }
        
        public Params setItemTitle(int itemPos, CharSequence title) {
            itemTitles.put(itemPos, title);
            return this;
        }
    }
}