Home | History | Annotate | Download | only in bars
      1 /*
      2  * Copyright (C) 2014 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 
     17 package com.android.layoutlib.bridge.bars;
     18 
     19 import com.android.internal.view.menu.MenuBuilder;
     20 import com.android.internal.view.menu.MenuItemImpl;
     21 import com.android.internal.view.menu.MenuView;
     22 
     23 import android.content.Context;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.widget.BaseAdapter;
     28 
     29 import java.util.ArrayList;
     30 
     31 /**
     32  * Provides an adapter for Overflow menu popup. This is very similar to
     33  * {@code MenuPopupHelper.MenuAdapter}
     34  */
     35 public class OverflowMenuAdapter extends BaseAdapter {
     36 
     37     private final MenuBuilder mMenu;
     38     private int mExpandedIndex = -1;
     39     private final Context context;
     40 
     41     public OverflowMenuAdapter(MenuBuilder menu, Context context) {
     42         mMenu = menu;
     43         findExpandedIndex();
     44         this.context = context;
     45     }
     46 
     47     @Override
     48     public int getCount() {
     49         ArrayList<MenuItemImpl> items = mMenu.getNonActionItems();
     50         if (mExpandedIndex < 0) {
     51             return items.size();
     52         }
     53         return items.size() - 1;
     54     }
     55 
     56     @Override
     57     public MenuItemImpl getItem(int position) {
     58         ArrayList<MenuItemImpl> items = mMenu.getNonActionItems();
     59         if (mExpandedIndex >= 0 && position >= mExpandedIndex) {
     60             position++;
     61         }
     62         return items.get(position);
     63     }
     64 
     65     @Override
     66     public long getItemId(int position) {
     67         // Since a menu item's ID is optional, we'll use the position as an
     68         // ID for the item in the AdapterView
     69         return position;
     70     }
     71 
     72     @Override
     73     public View getView(int position, View convertView, ViewGroup parent) {
     74         if (convertView == null) {
     75             LayoutInflater mInflater = LayoutInflater.from(context);
     76             convertView = mInflater.inflate(com.android.internal.R.layout.popup_menu_item_layout,
     77                     parent, false);
     78         }
     79 
     80         MenuView.ItemView itemView = (MenuView.ItemView) convertView;
     81         itemView.initialize(getItem(position), 0);
     82         return convertView;
     83     }
     84 
     85     private void findExpandedIndex() {
     86         final MenuItemImpl expandedItem = mMenu.getExpandedItem();
     87         if (expandedItem != null) {
     88             final ArrayList<MenuItemImpl> items = mMenu.getNonActionItems();
     89             final int count = items.size();
     90             for (int i = 0; i < count; i++) {
     91                 final MenuItemImpl item = items.get(i);
     92                 if (item == expandedItem) {
     93                     mExpandedIndex = i;
     94                     return;
     95                 }
     96             }
     97         }
     98     }
     99 }
    100