Home | History | Annotate | Download | only in browseractions
      1 /*
      2  * Copyright 2018 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 androidx.browser.browseractions;
     18 
     19 import android.content.Context;
     20 import android.graphics.drawable.Drawable;
     21 import android.view.LayoutInflater;
     22 import android.view.View;
     23 import android.view.ViewGroup;
     24 import android.widget.BaseAdapter;
     25 import android.widget.ImageView;
     26 import android.widget.TextView;
     27 
     28 import androidx.browser.R;
     29 import androidx.core.content.res.ResourcesCompat;
     30 
     31 import java.util.List;
     32 
     33 /**
     34  * The adapter to display the icon and title of custom Browser Actions item.
     35  */
     36 class BrowserActionsFallbackMenuAdapter extends BaseAdapter {
     37     private final List<BrowserActionItem> mMenuItems;
     38     private final Context mContext;
     39 
     40     BrowserActionsFallbackMenuAdapter(List<BrowserActionItem> menuItems, Context context) {
     41         mMenuItems = menuItems;
     42         mContext = context;
     43     }
     44 
     45     @Override
     46     public int getCount() {
     47         return mMenuItems.size();
     48     }
     49 
     50     @Override
     51     public Object getItem(int position) {
     52         return mMenuItems.get(position);
     53     }
     54 
     55     @Override
     56     public long getItemId(int position) {
     57         return position;
     58     }
     59 
     60     @Override
     61     public View getView(int position, View convertView, ViewGroup parent) {
     62         final BrowserActionItem menuItem = mMenuItems.get(position);
     63         ViewHolderItem viewHolder;
     64         if (convertView == null) {
     65             convertView = LayoutInflater.from(mContext).inflate(
     66                     R.layout.browser_actions_context_menu_row, null);
     67             viewHolder = new ViewHolderItem();
     68             viewHolder.mIcon =
     69                     (ImageView) convertView.findViewById(R.id.browser_actions_menu_item_icon);
     70             viewHolder.mText =
     71                     (TextView) convertView.findViewById(R.id.browser_actions_menu_item_text);
     72             convertView.setTag(viewHolder);
     73         } else {
     74             viewHolder = (ViewHolderItem) convertView.getTag();
     75         }
     76 
     77         viewHolder.mText.setText(menuItem.getTitle());
     78         if (menuItem.getIconId() != 0) {
     79             Drawable drawable = ResourcesCompat.getDrawable(
     80                     mContext.getResources(), menuItem.getIconId(), null);
     81             viewHolder.mIcon.setImageDrawable(drawable);
     82         } else {
     83             viewHolder.mIcon.setImageDrawable(null);
     84         }
     85         return convertView;
     86     }
     87 
     88     private static class ViewHolderItem {
     89         ImageView mIcon;
     90         TextView mText;
     91     }
     92 }
     93