Home | History | Annotate | Download | only in input
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 package org.chromium.content.browser.input;
      6 
      7 import android.content.Context;
      8 import android.view.View;
      9 import android.view.ViewGroup;
     10 import android.widget.ArrayAdapter;
     11 import android.widget.CheckedTextView;
     12 import android.widget.TextView;
     13 
     14 import java.util.ArrayList;
     15 import java.util.List;
     16 
     17 /**
     18  * Select popup item adapter for SelectPopupDialog, used so we can disable
     19  * OPTION_GROUP items.
     20  */
     21 public class SelectPopupAdapter extends ArrayAdapter<SelectPopupItem> {
     22     // Holds the items of the select popup alert dialog list.
     23     private List<SelectPopupItem> mItems;
     24 
     25     // True if all items have type PopupItemType.ENABLED.
     26     private boolean mAreAllItemsEnabled;
     27 
     28     /**
     29      * Creates a new SelectPopupItem adapter for the select popup alert dialog list.
     30      * @param context        Application context.
     31      * @param layoutResource Layout resource used for the alert dialog list.
     32      * @param items          SelectPopupItem array list.
     33      */
     34     public SelectPopupAdapter(Context context, int layoutResource,
     35             List<SelectPopupItem> items) {
     36         super(context, layoutResource, items);
     37         mItems = new ArrayList<SelectPopupItem>(items);
     38 
     39         mAreAllItemsEnabled = true;
     40         for (int i = 0; i < mItems.size(); i++) {
     41             if (mItems.get(i).getType() != PopupItemType.ENABLED) {
     42                 mAreAllItemsEnabled = false;
     43                 break;
     44             }
     45         }
     46     }
     47 
     48     @Override
     49     public View getView(int position, View convertView, ViewGroup parent) {
     50         if (position < 0 || position >= getCount()) return null;
     51 
     52         // Always pass in null so that we will get a new CheckedTextView. Otherwise, an item
     53         // which was previously used as an <optgroup> element (i.e. has no check), could get
     54         // used as an <option> element, which needs a checkbox/radio, but it would not have
     55         // one.
     56         convertView = super.getView(position, null, parent);
     57         ((TextView) convertView).setText(mItems.get(position).getLabel());
     58 
     59         if (mItems.get(position).getType() != PopupItemType.ENABLED) {
     60             if (mItems.get(position).getType() == PopupItemType.GROUP) {
     61                 // Currently select_dialog_multichoice uses CheckedTextViews.
     62                 // If that changes, the class cast will no longer be valid.
     63                 // The WebView build cannot rely on this being the case, so
     64                 // we must check.
     65                 if (convertView instanceof CheckedTextView) {
     66                     ((CheckedTextView) convertView).setCheckMarkDrawable(null);
     67                 }
     68             } else {
     69                 // Draw the disabled element in a disabled state.
     70                 convertView.setEnabled(false);
     71             }
     72         }
     73         return convertView;
     74     }
     75 
     76     @Override
     77     public boolean areAllItemsEnabled() {
     78         return mAreAllItemsEnabled;
     79     }
     80 
     81     @Override
     82     public boolean isEnabled(int position) {
     83         if (position < 0 || position >= getCount()) return false;
     84         return mItems.get(position).getType() == PopupItemType.ENABLED;
     85     }
     86 }
     87