1929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta/*
2929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta * Copyright (C) 2014 The Android Open Source Project
3929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta *
4929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta * Licensed under the Apache License, Version 2.0 (the "License");
5929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta * you may not use this file except in compliance with the License.
6929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta * You may obtain a copy of the License at
7929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta *
8929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta *      http://www.apache.org/licenses/LICENSE-2.0
9929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta *
10929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta * Unless required by applicable law or agreed to in writing, software
11929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta * distributed under the License is distributed on an "AS IS" BASIS,
12929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta * See the License for the specific language governing permissions and
14929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta * limitations under the License.
15929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta */
16929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
17929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptapackage com.android.layoutlib.bridge.bars;
18929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
19929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport com.android.internal.view.menu.MenuBuilder;
20929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport com.android.internal.view.menu.MenuItemImpl;
21929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport com.android.internal.view.menu.MenuView;
22929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
23929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport android.content.Context;
24929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport android.view.LayoutInflater;
25929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport android.view.View;
26929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport android.view.ViewGroup;
27929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport android.widget.BaseAdapter;
28929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
29929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptaimport java.util.ArrayList;
30929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
31929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta/**
32929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta * Provides an adapter for Overflow menu popup. This is very similar to
33929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta * {@code MenuPopupHelper.MenuAdapter}
34929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta */
35929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Guptapublic class OverflowMenuAdapter extends BaseAdapter {
36929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
37929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    private final MenuBuilder mMenu;
38929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    private int mExpandedIndex = -1;
39929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    private final Context context;
40929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
41929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    public OverflowMenuAdapter(MenuBuilder menu, Context context) {
42929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        mMenu = menu;
43929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        findExpandedIndex();
44929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        this.context = context;
45929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    }
46929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
47929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    @Override
48929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    public int getCount() {
49929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        ArrayList<MenuItemImpl> items = mMenu.getNonActionItems();
50929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        if (mExpandedIndex < 0) {
51929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            return items.size();
52929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        }
53929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        return items.size() - 1;
54929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    }
55929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
56929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    @Override
57929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    public MenuItemImpl getItem(int position) {
58929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        ArrayList<MenuItemImpl> items = mMenu.getNonActionItems();
59929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        if (mExpandedIndex >= 0 && position >= mExpandedIndex) {
60929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            position++;
61929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        }
62929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        return items.get(position);
63929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    }
64929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
65929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    @Override
66929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    public long getItemId(int position) {
67929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        // Since a menu item's ID is optional, we'll use the position as an
68929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        // ID for the item in the AdapterView
69929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        return position;
70929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    }
71929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
72929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    @Override
73929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    public View getView(int position, View convertView, ViewGroup parent) {
74929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        if (convertView == null) {
75929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            LayoutInflater mInflater = LayoutInflater.from(context);
76929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            convertView = mInflater.inflate(com.android.internal.R.layout.popup_menu_item_layout,
77929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                    parent, false);
78929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        }
79929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
80929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        MenuView.ItemView itemView = (MenuView.ItemView) convertView;
81929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        itemView.initialize(getItem(position), 0);
82929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        return convertView;
83929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    }
84929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta
85929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    private void findExpandedIndex() {
86929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        final MenuItemImpl expandedItem = mMenu.getExpandedItem();
87929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        if (expandedItem != null) {
88929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            final ArrayList<MenuItemImpl> items = mMenu.getNonActionItems();
89929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            final int count = items.size();
90929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            for (int i = 0; i < count; i++) {
91929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                final MenuItemImpl item = items.get(i);
92929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                if (item == expandedItem) {
93929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                    mExpandedIndex = i;
94929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                    return;
95929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta                }
96929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta            }
97929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta        }
98929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta    }
99929eea6bc1824bf98d244550c0984ecf8ea98811Deepanshu Gupta}
100