| package com.android.dialer.list; |
| |
| import android.content.Context; |
| import android.content.res.Resources; |
| import android.telephony.PhoneNumberUtils; |
| import android.view.View; |
| import android.view.ViewGroup; |
| |
| import com.android.contacts.common.GeoUtil; |
| import com.android.contacts.common.list.ContactListItemView; |
| import com.android.contacts.common.list.PhoneNumberListAdapter; |
| import com.android.dialer.R; |
| |
| /** |
| * {@link PhoneNumberListAdapter} with the following added shortcuts, that are displayed as list |
| * items: |
| * 1) Directly calling the phone number query |
| * 2) Adding the phone number query to a contact |
| * |
| * These shortcuts can be enabled or disabled to toggle whether or not they show up in the |
| * list. |
| */ |
| public class DialerPhoneNumberListAdapter extends PhoneNumberListAdapter { |
| |
| private String mFormattedQueryString; |
| private String mCountryIso; |
| |
| public final static int SHORTCUT_INVALID = -1; |
| public final static int SHORTCUT_DIRECT_CALL = 0; |
| public final static int SHORTCUT_ADD_NUMBER_TO_CONTACTS = 1; |
| public final static int SHORTCUT_MAKE_VIDEO_CALL = 2; |
| |
| public final static int SHORTCUT_COUNT = 3; |
| |
| private final boolean[] mShortcutEnabled = new boolean[SHORTCUT_COUNT]; |
| |
| public DialerPhoneNumberListAdapter(Context context) { |
| super(context); |
| |
| mCountryIso = GeoUtil.getCurrentCountryIso(context); |
| |
| // Enable all shortcuts by default |
| for (int i = 0; i < mShortcutEnabled.length; i++) { |
| mShortcutEnabled[i] = true; |
| } |
| } |
| |
| @Override |
| public int getCount() { |
| return super.getCount() + getShortcutCount(); |
| } |
| |
| /** |
| * @return The number of enabled shortcuts. Ranges from 0 to a maximum of SHORTCUT_COUNT |
| */ |
| public int getShortcutCount() { |
| int count = 0; |
| for (int i = 0; i < mShortcutEnabled.length; i++) { |
| if (mShortcutEnabled[i]) count++; |
| } |
| return count; |
| } |
| |
| @Override |
| public int getItemViewType(int position) { |
| final int shortcut = getShortcutTypeFromPosition(position); |
| if (shortcut >= 0) { |
| // shortcutPos should always range from 1 to SHORTCUT_COUNT |
| return super.getViewTypeCount() + shortcut; |
| } else { |
| return super.getItemViewType(position); |
| } |
| } |
| |
| @Override |
| public int getViewTypeCount() { |
| // Number of item view types in the super implementation + 2 for the 2 new shortcuts |
| return super.getViewTypeCount() + SHORTCUT_COUNT; |
| } |
| |
| @Override |
| public View getView(int position, View convertView, ViewGroup parent) { |
| final int shortcutType = getShortcutTypeFromPosition(position); |
| if (shortcutType >= 0) { |
| if (convertView != null) { |
| assignShortcutToView((ContactListItemView) convertView, shortcutType); |
| return convertView; |
| } else { |
| final ContactListItemView v = new ContactListItemView(getContext(), null); |
| assignShortcutToView(v, shortcutType); |
| return v; |
| } |
| } else { |
| return super.getView(position, convertView, parent); |
| } |
| } |
| |
| /** |
| * @param position The position of the item |
| * @return The enabled shortcut type matching the given position if the item is a |
| * shortcut, -1 otherwise |
| */ |
| public int getShortcutTypeFromPosition(int position) { |
| int shortcutCount = position - super.getCount(); |
| if (shortcutCount >= 0) { |
| // Iterate through the array of shortcuts, looking only for shortcuts where |
| // mShortcutEnabled[i] is true |
| for (int i = 0; shortcutCount >= 0 && i < mShortcutEnabled.length; i++) { |
| if (mShortcutEnabled[i]) { |
| shortcutCount--; |
| if (shortcutCount < 0) return i; |
| } |
| } |
| throw new IllegalArgumentException("Invalid position - greater than cursor count " |
| + " but not a shortcut."); |
| } |
| return SHORTCUT_INVALID; |
| } |
| |
| @Override |
| public boolean isEmpty() { |
| return getShortcutCount() == 0 && super.isEmpty(); |
| } |
| |
| @Override |
| public boolean isEnabled(int position) { |
| final int shortcutType = getShortcutTypeFromPosition(position); |
| if (shortcutType >= 0) { |
| return true; |
| } else { |
| return super.isEnabled(position); |
| } |
| } |
| |
| private void assignShortcutToView(ContactListItemView v, int shortcutType) { |
| final CharSequence text; |
| final int drawableId; |
| final Resources resources = getContext().getResources(); |
| final String number = getFormattedQueryString(); |
| switch (shortcutType) { |
| case SHORTCUT_DIRECT_CALL: |
| text = resources.getString(R.string.search_shortcut_call_number, number); |
| drawableId = R.drawable.ic_search_phone; |
| break; |
| case SHORTCUT_ADD_NUMBER_TO_CONTACTS: |
| text = resources.getString(R.string.search_shortcut_add_to_contacts); |
| drawableId = R.drawable.ic_search_add_contact; |
| break; |
| case SHORTCUT_MAKE_VIDEO_CALL: |
| text = resources.getString(R.string.search_shortcut_make_video_call); |
| drawableId = R.drawable.ic_videocam; |
| break; |
| default: |
| throw new IllegalArgumentException("Invalid shortcut type"); |
| } |
| v.setDrawableResource(R.drawable.search_shortcut_background, drawableId); |
| v.setDisplayName(text); |
| v.setPhotoPosition(super.getPhotoPosition()); |
| v.setAdjustSelectionBoundsEnabled(false); |
| } |
| |
| public void setShortcutEnabled(int shortcutType, boolean visible) { |
| mShortcutEnabled[shortcutType] = visible; |
| } |
| |
| public String getFormattedQueryString() { |
| return mFormattedQueryString; |
| } |
| |
| @Override |
| public void setQueryString(String queryString) { |
| mFormattedQueryString = PhoneNumberUtils.formatNumber( |
| PhoneNumberUtils.normalizeNumber(queryString), mCountryIso); |
| super.setQueryString(queryString); |
| } |
| } |