summaryrefslogtreecommitdiffstats
path: root/src/com/android/contacts/multipicker/SearchFragment.java
blob: b33ddf836052741d75cd930dc15b794d38877340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
/*
 * Copyright (c) 2016, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of The Linux Foundation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.android.contacts.multipicker;

import android.accounts.Account;
import android.app.Activity;
import android.app.ListFragment;
import android.content.AsyncQueryHandler;
import android.content.ComponentName;
import android.content.Context;
import android.database.Cursor;
import android.database.MergeCursor;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog.Calls;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.Directory;
import android.provider.ContactsContract.Groups;
import android.provider.ContactsContract.QuickContact;
import android.provider.ContactsContract.RawContacts;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.telephony.PhoneNumberUtils;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.contacts.activities.MultiPickContactsActivity;
import com.android.contacts.R;
import com.android.contacts.common.ContactPhotoManager;
import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
import com.android.contacts.common.MoreContactUtils;
import com.android.contacts.common.SimContactsConstants;
import com.android.contacts.common.format.TextHighlighter;
import com.android.contacts.common.util.ContactDisplayUtils;
import com.android.contacts.common.util.SearchUtil;
import com.android.contacts.common.util.UriUtils;
import com.android.contacts.common.widget.CheckableImageView;
import com.android.contacts.list.ContactsPickMode;
import com.android.contacts.list.OnCheckListActionListener;
import com.google.common.collect.Sets;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Set;

public class SearchFragment extends ListFragment {
    private final static String TAG = "SearchFragment";
    private final static boolean DEBUG = true;

    private static final String SORT_ORDER = " desc";

    static final String[] PHONES_PROJECTION = new String[] {
            Phone._ID, // 0
            Phone.TYPE, // 1
            Phone.LABEL, // 2
            Phone.NUMBER, // 3
            Phone.DISPLAY_NAME, // 4
            Contacts.PHOTO_ID, // 5
            Contacts.LOOKUP_KEY, // 6
            Phone.CONTACT_ID, // 7
            RawContacts.ACCOUNT_TYPE, // 8
            RawContacts.ACCOUNT_NAME, // 9
            Contacts.PHOTO_THUMBNAIL_URI // 10
    };

    static final String PHONES_SELECTION = RawContacts.ACCOUNT_TYPE + "<>?";

    static final String[] PHONES_SELECTION_ARGS = {
            SimContactsConstants.ACCOUNT_TYPE_SIM
    };

    private final static String[] GROUP_PROJECTION = new String[] {
            Groups._ID, // 0
            Groups.TITLE, // 1
            Groups.SUMMARY_COUNT, // 2
            Groups.SUMMARY_WITH_PHONES, // 3
            Groups.ACCOUNT_NAME, // 4
            Groups.ACCOUNT_TYPE, // 5
            Groups.DATA_SET // 6
    };

    public static final String[] CALL_LOG_PROJECTION = new String[] {
            Calls._ID, // 0
            Calls.NUMBER, // 1
            Calls.CACHED_LOOKUP_URI, // 2
            Calls.CACHED_PHOTO_ID, // 3
            Calls.CACHED_PHOTO_URI, // 4
            Calls.CACHED_FORMATTED_NUMBER // 5
    };

    // phone column
    private static final int PHONE_COLUMN_ID = 0;
    private static final int PHONE_COLUMN_TYPE = 1;
    private static final int PHONE_COLUMN_LABEL = 2;
    private static final int PHONE_COLUMN_NUMBER = 3;
    private static final int PHONE_COLUMN_DISPLAY_NAME = 4;
    private static final int PHONE_COLUMN_PHOTO_ID = 5;
    private static final int PHONE_COLUMN_LOOKUP_KEY = 6;
    private static final int PHONE_COLUMN_CONTACT_ID = 7;
    private static final int PHONE_ACCOUNT_TYPE = 8;
    private static final int PHONE_ACCOUNT_NAME = 9;
    private static final int PHONE_COLUMN_PHOTO_URI = 10;
    // groups column
    private static final int GROUP_ID = 0;
    private static final int GROUP_TITLE = 1;
    private static final int GROUP_SUMMARY_COUNT = 2;
    private static final int GROUP_SUMMARY_WITH_PHONES = 3;
    private static final int GROUP_ACCOUNT_NAME = 4;
    private static final int GROUP_ACCOUNT_TYPE = 5;
    private static final int GROUP_DATA_SET = 6;
    // call log column
    public static final int ID = 0;
    public static final int NUMBER = 1;
    public static final int CACHED_LOOKUP_URI = 2;
    public static final int CACHED_PHOTO_ID = 3;
    public static final int CACHED_PHOTO_URI = 4;
    public static final int CACHED_FORMATTED_NUMBER = 5;

    private static final int QUERY_TOKEN = 42;
    private static final int CONTACT_QUERY_TOKEN = 43;
    private static final int GROUP_QUERY_TOKEN = 44;
    private static final int CALLLOG_QUERY_TOKEN = 45;

    // Include PHONE, GROUPS and CALL
    private static final int VIEW_TYPE_COUNT = 3;

    private QueryHandler mQueryHandler;

    private Context mContext;

    private ContactsPickMode mPickMode;
    private int mMode;

    private OnCheckListActionListener mCheckListListener;

    private ContactItemListAdapter mContactListAdapter;

    private String query;

    private String mFilter;

    private View mRootView;

    private ArrayList<Cursor> mCursors;

    private static final int TYPE_CONTACTS = 0;
    private static final int TYPE_GROUP = 1;
    private static final int TYPE_CALLLOG = 2;
    private static final int TYPE_GROUP_CONTACTS = 3;

    /**
     * control of whether show the contacts in SIM card, if intent has this flag,not show.
     */
    private static final String EXT_NOT_SHOW_SIM_FLAG = "not_sim_show";

    private static final int BUFFER_LENGTH = 500;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (mContactListAdapter == null) {
            mContactListAdapter = new ContactItemListAdapter(mContext);
            mCursors = new ArrayList<Cursor>();
        }
        setListAdapter(mContactListAdapter);
        mQueryHandler = new QueryHandler(mContext);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mPickMode = ContactsPickMode.getInstance();
        mMode = mPickMode.getMode();
        mContext = (MultiPickContactsActivity) activity;
    }

    public void setCheckListListener(OnCheckListActionListener checkListListener) {
        mCheckListListener = checkListListener;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        int Type = mContactListAdapter.getItemViewType(position);
        switch (Type) {
            case TYPE_GROUP:
                GroupItemCache groupCache = (GroupItemCache) v.getTag();
                if (mCheckListListener.onContainsGroupId(groupCache.id)) {
                    mCheckListListener.onRemoveGroupId(groupCache.id);
                } else {
                    mCheckListListener.addGroupId(groupCache.id);
                }
                Cursor cursor = getContactsDetailCursor(groupCache.id);

                if (cursor == null) {
                    break;
                }
                if (!cursor.moveToFirst()) {
                    break;
                }
                do {
                    String key = String.valueOf(cursor.getLong(PHONE_COLUMN_ID));
                    if (mCheckListListener.onContainsGroupId(groupCache.id)) {
                        if (!mCheckListListener.onContainsKey(key)) {
                            ContactItemCache cache = new ContactItemCache();
                            cache.id = cursor.getLong(PHONE_COLUMN_ID);
                            cache.name = cursor
                                    .getString(PHONE_COLUMN_DISPLAY_NAME);
                            cache.number = cursor.getString(PHONE_COLUMN_NUMBER);
                            cache.type = cursor.getString(PHONE_COLUMN_TYPE);
                            cache.label = cursor.getString(PHONE_COLUMN_LABEL);
                            cache.contact_id = cursor
                                    .getString(PHONE_COLUMN_CONTACT_ID);
                            String[] value = null;
                            value = new String[] {
                                    cache.name, cache.number,
                                    cache.type, cache.label, cache.contact_id
                            };
                            mCheckListListener.putValue(key, value);
                        }
                    } else {
                        if (mCheckListListener.onContainsKey(key)) {
                            mCheckListListener.onRemove(key);
                        }
                    }
                } while (cursor.moveToNext());
                break;
            case TYPE_CONTACTS:
                ContactItemCache cache = (ContactItemCache) v.getTag();
                String key = String.valueOf(cache.id);

                if (!mCheckListListener.onContainsKey(key)) {
                    String[] value = null;
                    value = new String[] {
                            cache.name, cache.number, cache.type,
                            cache.label, cache.contact_id
                    };
                    mCheckListListener.putValue(key, value);
                } else {
                    mCheckListListener.onRemove(key);
                }
                break;
            case TYPE_CALLLOG:
                /* Click searched call log */
                PhoneCallDetails details = (PhoneCallDetails) v.getTag();
                String callLogKey = String.valueOf(details.mCallId);
                if (null != details) {

                    if (!mCheckListListener.onContainsNumberKey(callLogKey)) {
                        String[] value;
                        value = new String[] {
                                details.mNumber
                        };
                        mCheckListListener.putNumberValue(callLogKey, value);
                    } else {
                        mCheckListListener.onNumberRemove(callLogKey);
                    }
                }
                break;
            default:
                break;
        }

        mCheckListListener.onUpdateActionBar();
        mCheckListListener.exitSearch();
    }

    @Override
    public void onDetach() {
        super.onDetach();

        mContext = null;
    }

    @Override
    public void onDestroy() {
        mQueryHandler.removeCallbacksAndMessages(QUERY_TOKEN);
        mQueryHandler.removeCallbacksAndMessages(CONTACT_QUERY_TOKEN);
        mQueryHandler.removeCallbacksAndMessages(GROUP_QUERY_TOKEN);
        mQueryHandler.removeCallbacksAndMessages(CALLLOG_QUERY_TOKEN);

        if (mContactListAdapter.getCursor() != null) {
            mContactListAdapter.getCursor().close();
        }

        super.onDestroy();
    }

    private Uri getUriToQuery() {
        Uri uri;
        switch (mMode) {
            case ContactsPickMode.MODE_DEFAULT_PHONE:
            case ContactsPickMode.MODE_SEARCH_PHONE:
                uri = Phone.CONTENT_URI;
                break;
            case ContactsPickMode.MODE_DEFAULT_CALL:
            case ContactsPickMode.MODE_SEARCH_CALL:
                uri = Calls.CONTENT_URI;
                break;
            /*
             * case ContactsPickMode.MODE_SEARCH_GROUP: uri = Groups.CONTENT_SUMMARY_URI; break;
             */
            default:
                throw new IllegalArgumentException("getUriToQuery: Incorrect mode: " + mMode);
        }
        return uri.buildUpon().appendQueryParameter(Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true")
                .build();
    }

    private Uri getFilterUri() {
        return Phone.CONTENT_FILTER_URI;
    }

    public String[] getProjectionForQuery() {
        switch (mMode) {
            case ContactsPickMode.MODE_SEARCH_PHONE:
                return PHONES_PROJECTION;
            case ContactsPickMode.MODE_SEARCH_CALL:
                return CALL_LOG_PROJECTION;
            case ContactsPickMode.MODE_SEARCH_GROUP:
                return GROUP_PROJECTION;
            default:
                log("getProjectionForQuery: Incorrect mode: " + mMode);
        }
        return PHONES_PROJECTION;
    }

    private String getSortOrder() {
        switch (mMode) {
            case ContactsPickMode.MODE_SEARCH_CALL:
                return CALL_LOG_PROJECTION[2] + SORT_ORDER;
            case ContactsPickMode.MODE_SEARCH_GROUP:
                return createGroupSortOrder();
            default:
                return RawContacts.SORT_KEY_PRIMARY;
        }
    }

    private String getSelectionForQuery() {
        switch (mMode) {
            case ContactsPickMode.MODE_SEARCH_PHONE:
                if (isShowSIM()) {
                    return null;
                } else {
                    return PHONES_SELECTION;
                }
            case ContactsPickMode.MODE_SEARCH_GROUP:
                return createGroupSelection(query);
            case ContactsPickMode.MODE_SEARCH_CALL:
                return createCallLogSelection(query);
            default:
                return null;
        }
    }

    private int getQueryToken() {
        switch (mMode) {
            case ContactsPickMode.MODE_SEARCH_GROUP:
                return GROUP_QUERY_TOKEN;
            case ContactsPickMode.MODE_SEARCH_PHONE:
                return CONTACT_QUERY_TOKEN;
            case ContactsPickMode.MODE_SEARCH_CALL:
                return CALLLOG_QUERY_TOKEN;
            default:
                return QUERY_TOKEN;
        }
    }

    private String getQueryStr() {
        switch (mMode) {
            case ContactsPickMode.MODE_SEARCH_GROUP:
            case ContactsPickMode.MODE_SEARCH_PHONE:
            case ContactsPickMode.MODE_SEARCH_CALL:
                return query;
            default:
                return null;
        }
    }

    private String[] getSelectionArgsForQuery() {
        switch (mMode) {
            case ContactsPickMode.MODE_SEARCH_PHONE:
                if (isShowSIM()) {
                    return null;
                } else {
                    return PHONES_SELECTION_ARGS;
                }
            default:
                return null;
        }
    }

    private boolean isShowSIM() {
        // if airplane mode on, do not show SIM.
        return !mPickMode.getIntent().hasExtra(EXT_NOT_SHOW_SIM_FLAG);
    }

    public void doFilter(int mode, String s) {
        query = s;

        if (TextUtils.isEmpty(s)) {
            mContactListAdapter.changeCursor(null);
            return;
        }

        Uri uri = null;
        switch (mode) {
            case ContactsPickMode.MODE_SEARCH_GROUP:
                mMode = mode;
                uri = Groups.CONTENT_SUMMARY_URI;
                break;
            case ContactsPickMode.MODE_SEARCH_PHONE:
                mMode = mode;
                uri = Uri.withAppendedPath(getFilterUri(), query);
                break;
            case ContactsPickMode.MODE_SEARCH_CALL:
                mMode = mode;
                uri = getUriToQuery();
                break;
            default:
                mMode = mode;
                uri = Uri.withAppendedPath(getFilterUri(), query);
                break;
        }

        mQueryHandler.startQuery(getQueryToken(), getQueryStr(), uri, getProjectionForQuery(),
                getSelectionForQuery(),
                getSelectionArgsForQuery(), getSortOrder());

    }

    private class QueryHandler extends AsyncQueryHandler {
        protected WeakReference<SearchFragment> mFragment;

        public QueryHandler(Context context) {
            super(context.getContentResolver());
            mFragment = new WeakReference<SearchFragment>((SearchFragment) SearchFragment.this);
        }

        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            // In the case of low memory, the WeakReference object may be
            // recycled.
            if (mFragment == null || mFragment.get() == null) {
                mFragment = new WeakReference<SearchFragment>(SearchFragment.this);
            }
            final SearchFragment fragment = mFragment.get();
            String filter = (String) cookie;
            // setFilterString(filter);
            switch (token) {
                case QUERY_TOKEN:
                    mContactListAdapter.changeCursor(cursor);
                    break;
                case GROUP_QUERY_TOKEN:
                    if (cursor != null) {
                        mCursors.add(cursor);
                    }
                    doFilter(ContactsPickMode.MODE_SEARCH_PHONE, filter);
                    break;
                case CONTACT_QUERY_TOKEN:
                    if (cursor != null) {
                        mCursors.add(cursor);
                    }
                    doFilter(ContactsPickMode.MODE_SEARCH_CALL, filter);
                    break;
                case CALLLOG_QUERY_TOKEN:
                    // Call log query complete.
                    if (cursor != null) {
                        mCursors.add(cursor);
                    }
                    if (mCursors.size() == 0) {
                        Toast.makeText(mContext, R.string.listFoundAllContactsZero,
                                Toast.LENGTH_SHORT).show();
                    } else {
                        Cursor[] cursors = new Cursor[mCursors.size()];
                        for (int i = 0; i < mCursors.size(); i++) {
                            cursors[i] = mCursors.get(i);
                        }
                        Cursor mergeCursor = new MergeCursor(cursors);
                        mMode = ContactsPickMode.MODE_SEARCH_PHONE;
                        mPickMode.setMode(mMode);
                        mContactListAdapter.changeCursor(mergeCursor);
                        if (mCursors != null) {
                            mCursors.clear();
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }

    private String getDisplayNumber(String number) {
        if (TextUtils.isEmpty(number)) {
            return "";
        }
        if (PhoneNumberUtils.isVoiceMailNumber(number.toString())) {
            return getString(R.string.voicemail);
        }
        return number;
    }

    private class ContactItemCache {
        long id;
        String name;
        String number;
        String lookupKey;
        String type;
        String label;
        String contact_id;
        String email;
        String anrs;
        long nameRawContactId;
        Uri photoUri;
        Uri contactUri;
        long photoId;
    }

    private class GroupItemCache {
        long id;
        String title;
        String summary_count;
        String summary_with_phones;
        String account_name;
        String account_type;
        int phone_numbers;
    }

    public class ContactItemListAdapter extends CursorAdapter {
        protected LayoutInflater mInflater;
        private ContactPhotoManager mContactPhotoManager;
        private final TextHighlighter mTextHighlighter;

        private int contactsNum;
        private int groupNum;
        private int calllogNum;

        public ContactItemListAdapter(Context context) {
            super(context, null, false);

            mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mContactPhotoManager = ContactPhotoManager.getInstance(mContext);
            mTextHighlighter = new TextHighlighter(Typeface.BOLD);
        }

        private void setLabel(View view, Cursor cursor) {
            TextView labelView = (TextView) view.findViewById(R.id.label);
            CharSequence label = null;
            int accountTypeIndex = cursor.getColumnIndexOrThrow(Phone.TYPE);
            int customLabelIndex = cursor.getColumnIndexOrThrow(Phone.LABEL);
            if (!cursor.isNull(accountTypeIndex)) {
                final int type = cursor.getInt(accountTypeIndex);
                final String customLabel = cursor.getString(customLabelIndex);

                label = Phone.getTypeLabel(mContext.getResources(), type, customLabel);
            }
            labelView.setText(label);
        }

        /**
         * Compose PhoneAccount object from component name and account id.
         */
        public PhoneAccountHandle getAccount(String componentString, String accountId) {
            if (TextUtils.isEmpty(componentString) || TextUtils.isEmpty(accountId)) {
                return null;
            }
            final ComponentName componentName = ComponentName.unflattenFromString(componentString);
            return new PhoneAccountHandle(componentName, accountId);
        }

        private void setItemView(View view, Cursor cursor, ContactItemCache cache) {
            CheckableImageView photoView = (CheckableImageView) view
                    .findViewById(R.id.pick_contact_photo);

            boolean isFirstEntry = true;
            int contactIdIndex = cursor.getColumnIndexOrThrow(Phone.CONTACT_ID);
            if (!cursor.isNull(contactIdIndex)) {
                long currentContactId = cursor.getLong(contactIdIndex);

                int position = cursor.getPosition();
                if (!cursor.isFirst() && cursor.moveToPrevious()
                        && (getItemViewType(position - 1) == TYPE_CONTACTS)) {
                    int preContactIdIndex = cursor.getColumnIndexOrThrow(Phone.CONTACT_ID);
                    if (!cursor.isNull(contactIdIndex)) {
                        final long previousContactId = cursor.getLong(preContactIdIndex);
                        if (currentContactId == previousContactId) {
                            isFirstEntry = false;
                        }
                    }
                }
                cursor.moveToPosition(position);
            }

            if (isFirstEntry) {
                view.getLayoutParams().height = mContext.getResources()
                        .getDimensionPixelSize(R.dimen.pick_contact_first_item_height);
                photoView.setVisibility(View.VISIBLE);
                view.findViewById(R.id.pick_contact_name).setVisibility(View.VISIBLE);
            } else {
                view.getLayoutParams().height = mContext.getResources()
                        .getDimensionPixelSize(R.dimen.pick_contact_same_item_height);
                if (mCheckListListener.onContainsKey(String.valueOf(cache.id))) {
                    photoView.setVisibility(View.VISIBLE);
                } else {
                    photoView.setVisibility(View.INVISIBLE);
                }
                view.findViewById(R.id.pick_contact_name).setVisibility(View.GONE);
            }
        }

        private void bindCallLogView(View view, PhoneCallDetails details) {
            // Set Strange call logs filter highlight
            CharSequence formattedNumber = mTextHighlighter.applyPrefixHighlight(
                    details.mFormattedNumber, query.toUpperCase());
            details.numberText.setVisibility(View.GONE);
            details.labelText.setVisibility(View.GONE);
            details.nameText.setText(formattedNumber);

            CheckableImageView photoView = details.photoView;

            if (details.mPhotoId != 0) {
                mContactPhotoManager.loadThumbnail(photoView, details.mPhotoId, false, true, null);
            } else {
                final Uri photoUri = details.mPhotoUri == null ? null : details.mPhotoUri;
                DefaultImageRequest request = null;
                if (photoUri == null) {
                    details.mLookupKey = UriUtils.getLookupKeyFromUri(details.mLookupUri);
                    request = new DefaultImageRequest(details.mDisplayNumber,
                            details.mLookupKey, true);
                }
                mContactPhotoManager.loadDirectoryPhoto(photoView, photoUri, false, true,
                        request);
            }

            photoView.setChecked(
                    mCheckListListener.onContainsNumberKey(String.valueOf(details.mCallId)), false);

            // Activate photo when photo is check.
            if (photoView.isChecked()) {
                view.setActivated(true);
                return;
            } else {
                view.setActivated(false);
            }
        }

        private void setPhotoView(View view, Cursor cursor, ContactItemCache cache) {
            CheckableImageView photoView = ((CheckableImageView) view
                    .findViewById(R.id.pick_contact_photo));
            photoView.setVisibility(View.VISIBLE);

            int photoIdIndex = cursor.getColumnIndexOrThrow(Contacts.PHOTO_ID);
            if (!cursor.isNull(photoIdIndex)) {
                cache.photoId = cursor.getLong(photoIdIndex);
            } else {
                cache.photoId = 0;
            }

            int photoUriIndex = cursor.getColumnIndexOrThrow(Contacts.PHOTO_THUMBNAIL_URI);
            if (!cursor.isNull(photoUriIndex)) {
                cache.photoUri = UriUtils.parseUriOrNull(cursor.getString(photoUriIndex));
            } else {
                cache.photoUri = null;
            }

            Account account = null;
            int accountNameIndex = cursor.getColumnIndexOrThrow(RawContacts.ACCOUNT_NAME);
            int accoutTypeIndex = cursor.getColumnIndexOrThrow(RawContacts.ACCOUNT_TYPE);
            if (!cursor.isNull(accoutTypeIndex) && !cursor.isNull(accountNameIndex)) {
                final String accountType = cursor.getString(accoutTypeIndex);
                final String accountName = cursor.getString(accountNameIndex);
                account = new Account(accountName, accountType);
            }

            if (cache.photoId != 0) {
                mContactPhotoManager.loadThumbnail(photoView, cache.photoId, account, false, true,
                        null);
            } else {
                final Uri photoUri = cache.photoUri == null ? null : cache.photoUri;
                DefaultImageRequest request = null;
                if (photoUri == null) {
                    cache.lookupKey = cursor
                            .getString(cursor.getColumnIndexOrThrow(Contacts.LOOKUP_KEY));
                    request = new DefaultImageRequest(cache.name, cache.lookupKey, true);
                }
                mContactPhotoManager.loadDirectoryPhoto(photoView, photoUri, account, false, true,
                        request);
            }

            photoView.setChecked(mCheckListListener.onContainsKey(String.valueOf(cache.id)), false);
            if (photoView.isChecked()) {
                view.setActivated(true);
            } else {
                view.setActivated(false);
            }
        }

        @Override
        public int getViewTypeCount() {
            return VIEW_TYPE_COUNT;
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            View v = null;

            int type = getItemViewType(cursor.getPosition());
            switch (type) {
                case TYPE_GROUP:
                    v = mInflater.inflate(R.layout.pick_group_list_item_view, parent, false);
                    GroupItemCache groupCache = new GroupItemCache();
                    v.setTag(groupCache);
                    break;
                case TYPE_CALLLOG:
                    // New call log's view.
                    v = mInflater.inflate(R.layout.multi_pick_contact_item, parent, false);
                    PhoneCallDetails details = new PhoneCallDetails(v);
                    v.setTag(details);
                    break;
                default:
                    v = mInflater.inflate(R.layout.multi_pick_contact_item, parent, false);
                    ContactItemCache dataCache = new ContactItemCache();
                    v.setTag(dataCache);
                    break;
            }

            return v;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View v;

            if (!getCursor().moveToPosition(position)) {
                throw new IllegalStateException("couldn't move cursor to position " + position);
            }

            if (convertView != null && convertView.getTag() != null) {
                v = convertView;
            } else {
                v = newView(mContext, getCursor(), parent);
            }
            bindView(v, mContext, getCursor());
            return v;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            int position = cursor.getPosition();
            int Type = getItemViewType(position);
            switch (Type) {
                case TYPE_GROUP:
                    GroupItemCache groupCache = (GroupItemCache) view.getTag();
                    groupCache.id = cursor.getLong(GROUP_ID);
                    groupCache.title = cursor.getString(GROUP_TITLE);
                    groupCache.summary_count = cursor.getString(GROUP_SUMMARY_COUNT);
                    groupCache.summary_with_phones = cursor.getString(GROUP_SUMMARY_WITH_PHONES);
                    String summary_count = mContext.getResources().getString(
                            R.string.summary_count_numbers,
                            groupCache.summary_with_phones);
                    setGroupChecked(view, cursor, groupCache, position);
                    setGroupFilterHighLight(view, groupCache);
                    break;
                case TYPE_CONTACTS:
                    hideSectionView(view);
                    ContactItemCache cache = (ContactItemCache) view.getTag();
                    cache.id = cursor.getLong(PHONE_COLUMN_ID);
                    cache.name = cursor.getString(PHONE_COLUMN_DISPLAY_NAME);
                    cache.number = cursor.getString(PHONE_COLUMN_NUMBER);
                    cache.label = cursor.getString(PHONE_COLUMN_LABEL);
                    cache.type = String.valueOf(cursor.getInt(PHONE_COLUMN_TYPE));

                    setPhotoView(view, cursor, cache);
                    setItemView(view, cursor, cache);
                    setLabel(view, cursor);
                    setFilterHighLight(view, cache.name, cache.number);
                    break;
                case TYPE_CALLLOG:
                    // Bind searched call log view
                    PhoneCallDetails details = (PhoneCallDetails) view.getTag();
                    bindCallLogData(details, cursor);
                    bindCallLogView(view, details);
                    break;
            }
        }

        private void setFilterHighLight(View view, String name, String number) {
            if (query == null) {
                return;
            }
            TextView numberView = (TextView) view.findViewById(R.id.pick_contact_number);
            CharSequence numberText = number;
            if (ContactDisplayUtils.isPossiblePhoneNumber(query)) {
                numberText = mTextHighlighter.applyPrefixHighlight(numberText, query.toUpperCase());
            }
            numberView.setText(numberText);

            TextView nameView = (TextView) view.findViewById(R.id.pick_contact_name);
            CharSequence nameText = name;
            nameText = mTextHighlighter.applyPrefixHighlight(nameText, query.toUpperCase());
            nameView.setText(nameText);
        }

        private void setGroupFilterHighLight(View view, GroupItemCache groupCache) {
            TextView groupView = (TextView) view.findViewById(R.id.name);
            CharSequence groupNameText = groupCache.title;
            groupNameText = mTextHighlighter.applyPrefixHighlight(groupNameText,
                    query.toUpperCase());
            groupView.setText(groupNameText);
        }

        private void bindCallLogData(PhoneCallDetails details, Cursor cursor) {

            details.mCallId = cursor.getLong(ID);

            details.mNumber = cursor.getString(NUMBER);
            details.mFormattedNumber = cursor.getString(CACHED_FORMATTED_NUMBER);
            details.mDisplayNumber = getDisplayNumber(details.mNumber);

            details.mPhotoId = cursor.getLong(CACHED_PHOTO_ID);
            details.mPhotoUri = UriUtils.parseUriOrNull(cursor.getString(CACHED_PHOTO_URI));

            details.mLookupUri = UriUtils.parseUriOrNull(
                    cursor.getString(CACHED_LOOKUP_URI));
            if (details.mLookupUri != null) {
                details.mLookupKey = UriUtils.getLookupKeyFromUri(details.mLookupUri);
            }
        }

        private void setGroupChecked(View view, Cursor cursor, GroupItemCache groupCache,
                int groupPosition) {
            if (groupCache.phone_numbers == 0) {
                Cursor c = getContactsDetailCursor(groupCache.id);
                groupCache.phone_numbers = c.getCount();
            }
            String summary_count = getResources()
                    .getString(R.string.summary_count_numbers,
                            String.valueOf(groupCache.phone_numbers));
            ((TextView) view.findViewById(R.id.number_count)).setText(summary_count);
            CheckableImageView photoView = (CheckableImageView) view
                    .findViewById(R.id.pick_contact_photo);
            String newTitle = cursor.getString(cursor.getColumnIndexOrThrow(Groups.TITLE));
            groupCache.title = newTitle;
            int res_id;
            switch (groupPosition % 6) {
                case 0:
                    res_id = R.drawable.group_icon_1;
                    break;
                case 1:
                    res_id = R.drawable.group_icon_2;
                    break;
                case 2:
                    res_id = R.drawable.group_icon_3;
                    break;
                case 3:
                    res_id = R.drawable.group_icon_4;
                    break;
                case 4:
                    res_id = R.drawable.group_icon_5;
                    break;
                case 5:
                    res_id = R.drawable.group_icon_6;
                    break;
                default:
                    res_id = R.drawable.group_icon_6;
                    break;
            }

            if (res_id != 0) {
                Drawable drawable = null;
                drawable = mContext.getDrawable(res_id);
                photoView.setImageDrawable(drawable);
                photoView.setChecked(mCheckListListener.onContainsGroupId(groupCache.id), false);
                if (photoView.isChecked()) {
                    view.setActivated(true);
                } else {
                    view.setActivated(false);
                }
            }
        }

        @Override
        public int getItemViewType(int position) {
            if (!getCursor().moveToPosition(position)) {
                throw new IllegalStateException("couldn't move cursor to position " + position);
            }
            int ColumnCounts = getCursor().getColumnCount();

            if (ColumnCounts == PHONES_PROJECTION.length) {
                return TYPE_CONTACTS;
            } else if (ColumnCounts == GROUP_PROJECTION.length) {
                return TYPE_GROUP;
            } else if (ColumnCounts == CALL_LOG_PROJECTION.length) {
                return TYPE_CALLLOG;
            } else {
                return TYPE_GROUP_CONTACTS;
            }
        }

        @Override
        public void changeCursor(Cursor cursor) {
            super.changeCursor(cursor);
        }

        private void hideSectionView(View view) {
            TextView section = (TextView) view.findViewById(R.id.section_index);
            section.setVisibility(View.GONE);
        }

    }

    protected static void log(String msg) {
        if (DEBUG) {
            Log.d(TAG, msg);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
        mRootView = inflater.inflate(R.layout.multi_pick_contacts_fragment, container, false);
        mRootView.setPadding(0, 0, 0, 0);
        return mRootView;
    }

    private String createGroupSelection(String query) {
        StringBuilder where = new StringBuilder();
        where.append(Groups.ACCOUNT_TYPE + " NOT NULL AND " + Groups.ACCOUNT_NAME + " NOT NULL AND "
                + Groups.AUTO_ADD
                + "=0 AND " + Groups.FAVORITES + "=0 AND " + Groups.DELETED + "!=1 AND "
                + Groups.TITLE + " like '%"
                + query + "%'");
        where.append(" AND (" + Groups.SUMMARY_WITH_PHONES + "<>0)");
        return where.toString();
    }

    private String createGroupSortOrder() {
            return Groups.SOURCE_ID;
    }

    private String createCallLogSelection(String filter) {
        String selection = Calls._ID + " in ( " + mCheckListListener.getCallLogSelection() + " )"
                + " and (" + Calls.NUMBER + " like '%" + filter + "%' and "
                + Calls.CACHED_NAME + " is null)";
        return selection;
    }

    private String createEmailOrNumberSelection() {
        StringBuilder selection = new StringBuilder();
        selection.append(ContactsContract.Data.MIMETYPE + "='" + Email.CONTENT_ITEM_TYPE + "'")
                .append(" OR ")
                .append(ContactsContract.Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'");
        return selection.toString();
    }

    private Cursor getContactsDetailCursor(long groupId) {

        StringBuilder selection = new StringBuilder();
        selection.append(Data.RAW_CONTACT_ID + " IN (" + " SELECT DISTINCT " + Data.RAW_CONTACT_ID
                + " FROM view_data WHERE " + Data.MIMETYPE + "=?" + " AND "
                + GroupMembership.GROUP_ROW_ID + "=?)");

        Cursor cursor = mContext.getContentResolver().query(Phone.CONTENT_URI, PHONES_PROJECTION,
                selection.toString(),
                createSelectionArgs(groupId), null);

        return cursor;
    }

    private String[] createSelectionArgs(long groupId) {
        List<String> selectionArgs = new ArrayList<String>();
        selectionArgs.add(GroupMembership.CONTENT_ITEM_TYPE);
        selectionArgs.add(String.valueOf(groupId));
        return selectionArgs.toArray(new String[0]);
    }

}