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