1/*
2 * Copyright (C) 2015 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 */
16package com.android.internal.view.menu;
17
18import android.view.LayoutInflater;
19import android.view.View;
20import android.view.ViewGroup;
21import android.widget.BaseAdapter;
22
23import java.util.ArrayList;
24
25public class MenuAdapter extends BaseAdapter {
26    static final int ITEM_LAYOUT = com.android.internal.R.layout.popup_menu_item_layout;
27
28    MenuBuilder mAdapterMenu;
29
30    private int mExpandedIndex = -1;
31
32    private boolean mForceShowIcon;
33    private final boolean mOverflowOnly;
34    private final LayoutInflater mInflater;
35
36    public MenuAdapter(MenuBuilder menu, LayoutInflater inflater, boolean overflowOnly) {
37        mOverflowOnly = overflowOnly;
38        mInflater = inflater;
39        mAdapterMenu = menu;
40        findExpandedIndex();
41    }
42
43    public boolean getForceShowIcon() {
44        return mForceShowIcon;
45    }
46
47    public void setForceShowIcon(boolean forceShow) {
48        mForceShowIcon = forceShow;
49    }
50
51    public int getCount() {
52        ArrayList<MenuItemImpl> items = mOverflowOnly ?
53                mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
54        if (mExpandedIndex < 0) {
55            return items.size();
56        }
57        return items.size() - 1;
58    }
59
60    public MenuBuilder getAdapterMenu() {
61        return mAdapterMenu;
62    }
63
64    public MenuItemImpl getItem(int position) {
65        ArrayList<MenuItemImpl> items = mOverflowOnly ?
66                mAdapterMenu.getNonActionItems() : mAdapterMenu.getVisibleItems();
67        if (mExpandedIndex >= 0 && position >= mExpandedIndex) {
68            position++;
69        }
70        return items.get(position);
71    }
72
73    public long getItemId(int position) {
74        // Since a menu item's ID is optional, we'll use the position as an
75        // ID for the item in the AdapterView
76        return position;
77    }
78
79    public View getView(int position, View convertView, ViewGroup parent) {
80        if (convertView == null) {
81            convertView = mInflater.inflate(ITEM_LAYOUT, parent, false);
82        }
83
84        MenuView.ItemView itemView = (MenuView.ItemView) convertView;
85        if (mForceShowIcon) {
86            ((ListMenuItemView) convertView).setForceShowIcon(true);
87        }
88        itemView.initialize(getItem(position), 0);
89        return convertView;
90    }
91
92    void findExpandedIndex() {
93        final MenuItemImpl expandedItem = mAdapterMenu.getExpandedItem();
94        if (expandedItem != null) {
95            final ArrayList<MenuItemImpl> items = mAdapterMenu.getNonActionItems();
96            final int count = items.size();
97            for (int i = 0; i < count; i++) {
98                final MenuItemImpl item = items.get(i);
99                if (item == expandedItem) {
100                    mExpandedIndex = i;
101                    return;
102                }
103            }
104        }
105        mExpandedIndex = -1;
106    }
107
108    @Override
109    public void notifyDataSetChanged() {
110        findExpandedIndex();
111        super.notifyDataSetChanged();
112    }
113}