Home | History | Annotate | Download | only in list
      1 package com.android.dialer.list;
      2 
      3 import android.content.Context;
      4 import android.content.res.Resources;
      5 import android.telephony.PhoneNumberUtils;
      6 import android.view.View;
      7 import android.view.ViewGroup;
      8 
      9 import com.android.contacts.common.GeoUtil;
     10 import com.android.contacts.common.list.ContactListItemView;
     11 import com.android.contacts.common.list.PhoneNumberListAdapter;
     12 import com.android.dialer.R;
     13 
     14 /**
     15  * {@link PhoneNumberListAdapter} with the following added shortcuts, that are displayed as list
     16  * items:
     17  * 1) Directly calling the phone number query
     18  * 2) Adding the phone number query to a contact
     19  *
     20  * These shortcuts can be enabled or disabled to toggle whether or not they show up in the
     21  * list.
     22  */
     23 public class DialerPhoneNumberListAdapter extends PhoneNumberListAdapter {
     24 
     25     private String mFormattedQueryString;
     26     private String mCountryIso;
     27 
     28     public final static int SHORTCUT_INVALID = -1;
     29     public final static int SHORTCUT_DIRECT_CALL = 0;
     30     public final static int SHORTCUT_ADD_NUMBER_TO_CONTACTS = 1;
     31 
     32     public final static int SHORTCUT_COUNT = 2;
     33 
     34     private final boolean[] mShortcutEnabled = new boolean[SHORTCUT_COUNT];
     35 
     36     public DialerPhoneNumberListAdapter(Context context) {
     37         super(context);
     38 
     39         mCountryIso = GeoUtil.getCurrentCountryIso(context);
     40 
     41         // Enable all shortcuts by default
     42         for (int i = 0; i < mShortcutEnabled.length; i++) {
     43             mShortcutEnabled[i] = true;
     44         }
     45     }
     46 
     47     @Override
     48     public int getCount() {
     49         return super.getCount() + getShortcutCount();
     50     }
     51 
     52     /**
     53      * @return The number of enabled shortcuts. Ranges from 0 to a maximum of SHORTCUT_COUNT
     54      */
     55     public int getShortcutCount() {
     56         int count = 0;
     57         for (int i = 0; i < mShortcutEnabled.length; i++) {
     58             if (mShortcutEnabled[i]) count++;
     59         }
     60         return count;
     61     }
     62 
     63     @Override
     64     public int getItemViewType(int position) {
     65         final int shortcut = getShortcutTypeFromPosition(position);
     66         if (shortcut >= 0) {
     67             // shortcutPos should always range from 1 to SHORTCUT_COUNT
     68             return super.getViewTypeCount() + shortcut;
     69         } else {
     70             return super.getItemViewType(position);
     71         }
     72     }
     73 
     74     @Override
     75     public int getViewTypeCount() {
     76         // Number of item view types in the super implementation + 2 for the 2 new shortcuts
     77         return super.getViewTypeCount() + SHORTCUT_COUNT;
     78     }
     79 
     80     @Override
     81     public View getView(int position, View convertView, ViewGroup parent) {
     82         final int shortcutType = getShortcutTypeFromPosition(position);
     83         if (shortcutType >= 0) {
     84             if (convertView != null) {
     85                 assignShortcutToView((ContactListItemView) convertView, shortcutType);
     86                 return convertView;
     87             } else {
     88                 final ContactListItemView v = new ContactListItemView(getContext(), null);
     89                 assignShortcutToView(v, shortcutType);
     90                 return v;
     91             }
     92         } else {
     93             return super.getView(position, convertView, parent);
     94         }
     95     }
     96 
     97     /**
     98      * @param position The position of the item
     99      * @return The enabled shortcut type matching the given position if the item is a
    100      * shortcut, -1 otherwise
    101      */
    102     public int getShortcutTypeFromPosition(int position) {
    103         int shortcutCount = position - super.getCount();
    104         if (shortcutCount >= 0) {
    105             // Iterate through the array of shortcuts, looking only for shortcuts where
    106             // mShortcutEnabled[i] is true
    107             for (int i = 0; shortcutCount >= 0 && i < mShortcutEnabled.length; i++) {
    108                 if (mShortcutEnabled[i]) {
    109                     shortcutCount--;
    110                     if (shortcutCount < 0) return i;
    111                 }
    112             }
    113             throw new IllegalArgumentException("Invalid position - greater than cursor count "
    114                     + " but not a shortcut.");
    115         }
    116         return SHORTCUT_INVALID;
    117     }
    118 
    119     @Override
    120     public boolean isEmpty() {
    121         return getShortcutCount() == 0 && super.isEmpty();
    122     }
    123 
    124     @Override
    125     public boolean isEnabled(int position) {
    126         final int shortcutType = getShortcutTypeFromPosition(position);
    127         if (shortcutType >= 0) {
    128             return true;
    129         } else {
    130             return super.isEnabled(position);
    131         }
    132     }
    133 
    134     private void assignShortcutToView(ContactListItemView v, int shortcutType) {
    135         final CharSequence text;
    136         final int drawableId;
    137         final Resources resources = getContext().getResources();
    138         final String number = getFormattedQueryString();
    139         switch (shortcutType) {
    140             case SHORTCUT_DIRECT_CALL:
    141                 text = resources.getString(R.string.search_shortcut_call_number, number);
    142                 drawableId = R.drawable.ic_phone_dk;
    143                 break;
    144             case SHORTCUT_ADD_NUMBER_TO_CONTACTS:
    145                 text = resources.getString(R.string.search_shortcut_add_to_contacts);
    146                 drawableId = R.drawable.ic_add_person_dk;
    147                 break;
    148             default:
    149                 throw new IllegalArgumentException("Invalid shortcut type");
    150         }
    151         v.setDrawableResource(R.drawable.list_item_avatar_bg, drawableId);
    152         v.setDisplayName(text);
    153         v.setPhotoPosition(super.getPhotoPosition());
    154     }
    155 
    156     public void setShortcutEnabled(int shortcutType, boolean visible) {
    157         mShortcutEnabled[shortcutType] = visible;
    158     }
    159 
    160     public String getFormattedQueryString() {
    161         return mFormattedQueryString;
    162     }
    163 
    164     @Override
    165     public void setQueryString(String queryString) {
    166         mFormattedQueryString = PhoneNumberUtils.formatNumber(
    167                 PhoneNumberUtils.convertAndStrip(queryString), mCountryIso);
    168         super.setQueryString(queryString);
    169     }
    170 }
    171