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