summaryrefslogtreecommitdiffstats
path: root/src/com/android/contacts/detail/ContactLoaderFragment.java
blob: e5decd764859beddd66a8b152e960568bb60d582 (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
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.contacts.detail;

import android.app.Activity;
import android.app.Fragment;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.Loader;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.provider.ContactsContract.CommonDataKinds.SipAddress;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.telephony.MSimTelephonyManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.contacts.ContactSaveService;
import com.android.contacts.R;
import com.android.contacts.activities.ContactDetailActivity.FragmentKeyListener;
import com.android.contacts.common.MoreContactUtils;
import com.android.contacts.common.SimContactsConstants;
import com.android.contacts.common.list.ShortcutIntentBuilder;
import com.android.contacts.common.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;
import com.android.contacts.common.model.Contact;
import com.android.contacts.common.model.ContactLoader;
import com.android.contacts.common.model.RawContact;
import com.android.contacts.common.model.RawContactDelta;
import com.android.contacts.common.model.dataitem.DataItem;
import com.android.contacts.common.model.dataitem.EmailDataItem;
import com.android.contacts.common.model.dataitem.PhoneDataItem;
import com.android.contacts.util.PhoneCapabilityTester;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;

import java.util.ArrayList;

/**
 * This is an invisible worker {@link Fragment} that loads the contact details for the contact card.
 * The data is then passed to the listener, who can then pass the data to other {@link View}s.
 */
public class ContactLoaderFragment extends Fragment implements FragmentKeyListener {

    private static final String TAG = ContactLoaderFragment.class.getSimpleName();

    /** The launch code when picking a ringtone */
    private static final int REQUEST_CODE_PICK_RINGTONE = 1;

    /** This is the Intent action to install a shortcut in the launcher. */
    private static final String ACTION_INSTALL_SHORTCUT =
            "com.android.launcher.action.INSTALL_SHORTCUT";

    private boolean mOptionsMenuOptions;
    private boolean mOptionsMenuEditable;
    private boolean mOptionsMenuShareable;
    private boolean mOptionsMenuCanCreateShortcut;
    private boolean mSendToVoicemailState;
    private String mCustomRingtone;

    private static final String ACTION_INSTALL_SHORTCUT_SUCCESSFUL =
            "com.android.launcher.action.INSTALL_SHORTCUT_SUCCESSFUL";
    private static final String EXTRA_RESPONSE_PACKAGENAME = "response_packagename";

    /**
     * This is a listener to the {@link ContactLoaderFragment} and will be notified when the
     * contact details have finished loading or if the user selects any menu options.
     */
    public static interface ContactLoaderFragmentListener {
        /**
         * Contact was not found, so somehow close this fragment. This is raised after a contact
         * is removed via Menu/Delete
         */
        public void onContactNotFound();

        /**
         * Contact details have finished loading.
         */
        public void onDetailsLoaded(Contact result);

        /**
         * User decided to go to Edit-Mode
         */
        public void onEditRequested(Uri lookupUri);

        /**
         * User decided to delete the contact
         */
        public void onDeleteRequested(Uri lookupUri);

    }

    private static final int LOADER_DETAILS = 1;

    private static final String KEY_CONTACT_URI = "contactUri";
    private static final String LOADER_ARG_CONTACT_URI = "contactUri";
    private static final String CONTACTS_COMMON_PKG_NAME = "com.android.contacts.common";

    private Context mContext;
    private Uri mLookupUri;
    private ContactLoaderFragmentListener mListener;

    private Contact mContactData;

    private IntentFilter mResponseFilter;

    /** Receive broadcast, show toast only when put shortcut sucessful in laucher */
    private BroadcastReceiver mResponseReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (!ACTION_INSTALL_SHORTCUT_SUCCESSFUL.equals(intent.getAction())) {
                return;
            }
            String packageName = intent.getStringExtra(EXTRA_RESPONSE_PACKAGENAME);
            if (packageName != null && (packageName.equals(context.getPackageName()) ||
                    CONTACTS_COMMON_PKG_NAME.equals(packageName))) {
                // Send a toast to give feedback to the user that a shortcut to this
                // contact was added to the launcher.
                Toast.makeText(context, R.string.createContactShortcutSuccessful,
                        Toast.LENGTH_SHORT).show();
            }
        }
    };

    public ContactLoaderFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mLookupUri = savedInstanceState.getParcelable(KEY_CONTACT_URI);
        }
        mResponseFilter = new IntentFilter(ACTION_INSTALL_SHORTCUT_SUCCESSFUL);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable(KEY_CONTACT_URI, mLookupUri);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
        setHasOptionsMenu(true);
        // This is an invisible view.  This fragment is declared in a layout, so it can't be
        // "viewless".  (i.e. can't return null here.)
        // See also the comment in the layout file.
        return inflater.inflate(R.layout.contact_detail_loader_fragment, container, false);
    }

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

        if (mLookupUri != null) {
            Bundle args = new Bundle();
            args.putParcelable(LOADER_ARG_CONTACT_URI, mLookupUri);
            getLoaderManager().initLoader(LOADER_DETAILS, args, mDetailLoaderListener);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        getActivity().unregisterReceiver(mResponseReceiver);
    }

    @Override
    public void onResume() {
        super.onResume();
        getActivity().registerReceiver(mResponseReceiver, mResponseFilter);
    }

    public void loadUri(Uri lookupUri) {
        if (Objects.equal(lookupUri, mLookupUri)) {
            // Same URI, no need to load the data again
            return;
        }

        mLookupUri = lookupUri;
        if (mLookupUri == null) {
            getLoaderManager().destroyLoader(LOADER_DETAILS);
            mContactData = null;
            if (mListener != null) {
                mListener.onDetailsLoaded(mContactData);
            }
        } else if (getActivity() != null) {
            Bundle args = new Bundle();
            args.putParcelable(LOADER_ARG_CONTACT_URI, mLookupUri);
            getLoaderManager().restartLoader(LOADER_DETAILS, args, mDetailLoaderListener);
        }
    }

    public void setListener(ContactLoaderFragmentListener value) {
        mListener = value;
    }

    /**
     * The listener for the detail loader
     */
    private final LoaderManager.LoaderCallbacks<Contact> mDetailLoaderListener =
            new LoaderCallbacks<Contact>() {
        @Override
        public Loader<Contact> onCreateLoader(int id, Bundle args) {
            Uri lookupUri = args.getParcelable(LOADER_ARG_CONTACT_URI);
            return new ContactLoader(mContext, lookupUri, true /* loadGroupMetaData */,
                    true /* load invitable account types */, true /* postViewNotification */,
                    true /* computeFormattedPhoneNumber */);
        }

        @Override
        public void onLoadFinished(Loader<Contact> loader, Contact data) {
            if (!mLookupUri.equals(data.getRequestedUri())) {
                Log.e(TAG, "Different URI: requested=" + mLookupUri + "  actual=" + data);
                return;
            }

            if (data.isError()) {
                // This shouldn't ever happen, so throw an exception. The {@link ContactLoader}
                // should log the actual exception.
                throw new IllegalStateException("Failed to load contact", data.getException());
            } else if (data.isNotFound()) {
                Log.i(TAG, "No contact found: " + ((ContactLoader)loader).getLookupUri());
                mContactData = null;
            } else {
                mContactData = data;
            }

            if (mListener != null) {
                if (mContactData == null) {
                    mListener.onContactNotFound();
                } else {
                    mListener.onDetailsLoaded(mContactData);
                }
            }
            // Make sure the options menu is setup correctly with the loaded data.
            if (getActivity() != null) getActivity().invalidateOptionsMenu();
        }

        @Override
        public void onLoaderReset(Loader<Contact> loader) {}
    };

    @Override
    public void onCreateOptionsMenu(Menu menu, final MenuInflater inflater) {
        inflater.inflate(R.menu.view_contact, menu);
    }

    public boolean isOptionsMenuChanged() {
        return mOptionsMenuOptions != isContactOptionsChangeEnabled()
                || mOptionsMenuEditable != isContactEditable()
                || mOptionsMenuShareable != isContactShareable()
                || mOptionsMenuCanCreateShortcut != isContactCanCreateShortcut();
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        mOptionsMenuOptions = isContactOptionsChangeEnabled();
        mOptionsMenuEditable = isContactEditable();
        mOptionsMenuShareable = isContactShareable();
        mOptionsMenuCanCreateShortcut = isContactCanCreateShortcut();
        if (mContactData != null) {
            mSendToVoicemailState = mContactData.isSendToVoicemail();
            mCustomRingtone = mContactData.getCustomRingtone();
        }

        // Hide telephony-related settings (ringtone, send to voicemail)
        // if we don't have a telephone
        final MenuItem optionsSendToVoicemail = menu.findItem(R.id.menu_send_to_voicemail);
        if (optionsSendToVoicemail != null) {
            optionsSendToVoicemail.setChecked(mSendToVoicemailState);
            optionsSendToVoicemail.setVisible(mOptionsMenuOptions);
        }
        final MenuItem optionsRingtone = menu.findItem(R.id.menu_set_ringtone);
        if (optionsRingtone != null) {
            optionsRingtone.setVisible(mOptionsMenuOptions);
        }

        final MenuItem editMenu = menu.findItem(R.id.menu_edit);
        if (editMenu != null) {
            editMenu.setVisible(mOptionsMenuEditable);
        }

        final MenuItem deleteMenu = menu.findItem(R.id.menu_delete);
        if (deleteMenu != null) {
            deleteMenu.setVisible(mOptionsMenuEditable);
        }

        final MenuItem shareMenu = menu.findItem(R.id.menu_share);
        if (shareMenu != null) {
            shareMenu.setVisible(mOptionsMenuShareable);
        }

        final MenuItem createContactShortcutMenu = menu.findItem(R.id.menu_create_contact_shortcut);
        if (createContactShortcutMenu != null) {
            createContactShortcutMenu.setVisible(mOptionsMenuCanCreateShortcut);
        }
        String accoutName = null;
        String accoutType = null;
        if (mContactData != null) {
            final RawContact rawContact = (RawContact) mContactData.getRawContacts().get(0);
            accoutName = rawContact.getAccountName();
            accoutType = rawContact.getAccountTypeString();
        }

        final MenuItem copyToPhoneMenu = menu.findItem(R.id.menu_copy_to_phone);
        if (copyToPhoneMenu != null) {
            copyToPhoneMenu.setVisible(false);
        }

        final MenuItem copyToSim1Menu = menu.findItem(R.id.menu_copy_to_sim1);
        if (copyToSim1Menu != null) {
            copyToSim1Menu.setVisible(false);
        }

        final MenuItem copyToSim2Menu = menu.findItem(R.id.menu_copy_to_sim2);
        if (copyToSim2Menu != null) {
            copyToSim2Menu.setVisible(false);
        }

        if (!TextUtils.isEmpty(accoutType)) {
            if (SimContactsConstants.ACCOUNT_TYPE_SIM.equals(accoutType)) {
                copyToPhoneMenu.setVisible(true);
                copyToPhoneMenu.setTitle(getString(R.string.menu_copyTo,
                        getString(R.string.phoneLabelsGroup)));
                if (MSimTelephonyManager.getDefault().isMultiSimEnabled()) {
                    if (SimContactsConstants.SIM_NAME_1.equals(accoutName)
                            && hasEnabledIccCard(SimContactsConstants.SUB_2)) {
                        copyToSim2Menu.setTitle(getString(R.string.menu_copyTo,
                                getString(R.string.copy_to_target_msim, 2)));
                        copyToSim2Menu.setVisible(true);
                    }
                    if (SimContactsConstants.SIM_NAME_2.equals(accoutName)
                            && hasEnabledIccCard(SimContactsConstants.SUB_1)) {
                        copyToSim1Menu.setTitle(getString(R.string.menu_copyTo,
                                getString(R.string.copy_to_target_msim, 1)));
                        copyToSim1Menu.setVisible(true);
                    }
                }
            } else if (SimContactsConstants.ACCOUNT_TYPE_PHONE.equals(accoutType)) {
                copyToPhoneMenu.setVisible(false);
                boolean hasPhoneOrEmail = hasPhoneOrEmailDate(mContactData);
                if (MSimTelephonyManager.getDefault().isMultiSimEnabled()) {
                    if (hasPhoneOrEmail && hasEnabledIccCard(SimContactsConstants.SUB_1)) {
                        copyToSim1Menu.setTitle(getString(R.string.menu_copyTo,
                                getString(R.string.copy_to_target_msim, 1)));
                        copyToSim1Menu.setVisible(true);
                    }
                    if (hasPhoneOrEmail && hasEnabledIccCard(SimContactsConstants.SUB_2)) {
                        copyToSim2Menu.setTitle(getString(R.string.menu_copyTo,
                                getString(R.string.copy_to_target_msim, 2)));
                        copyToSim2Menu.setVisible(true);
                    }
                } else {
                    if (hasPhoneOrEmail && TelephonyManager.getDefault().hasIccCard()
                            && TelephonyManager.getDefault().getSimState()
                                == TelephonyManager.SIM_STATE_READY) {
                        copyToSim1Menu.setTitle(getString(R.string.menu_copyTo,
                                getString(R.string.copy_to_target_sim)));
                        copyToSim1Menu.setVisible(true);
                    }
                }
            }

        }
    }
    private boolean hasPhoneOrEmailDate(Contact contact){
        int phoneCount = 0;
        int emailCount = 0;
        ImmutableList<RawContact> rawContacts = contact.getRawContacts();
        for (RawContact rawContact : rawContacts) {
            RawContactDelta rawContactDelta = RawContactDelta.fromBefore(rawContact);
            phoneCount += rawContactDelta.getMimeEntriesCount(
                    Phone.CONTENT_ITEM_TYPE, true);
            emailCount += rawContactDelta.getMimeEntriesCount(
                    Email.CONTENT_ITEM_TYPE, true);
        }
        if (phoneCount > 0 || emailCount > 0) {
            return true;
        } else {
            return false;
        }
    }

    private boolean hasEnabledIccCard(int subscription) {
        return MSimTelephonyManager.getDefault().hasIccCard(subscription)
                && MSimTelephonyManager.getDefault().getSimState(subscription)
                        == TelephonyManager.SIM_STATE_READY;
    }

    public boolean isContactOptionsChangeEnabled() {
        return mContactData != null && !mContactData.isDirectoryEntry()
                && PhoneCapabilityTester.isPhone(mContext);
    }

    public boolean isContactEditable() {
        return mContactData != null && !mContactData.isDirectoryEntry();
    }

    public boolean isContactShareable() {
        return mContactData != null && !mContactData.isDirectoryEntry();
    }

    public boolean isContactCanCreateShortcut() {
        return mContactData != null && !mContactData.isUserProfile()
                && !mContactData.isDirectoryEntry();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_edit: {
                if (mListener != null) mListener.onEditRequested(mLookupUri);
                break;
            }
            case R.id.menu_delete: {
                if (mListener != null) mListener.onDeleteRequested(mLookupUri);
                return true;
            }
            case R.id.menu_set_ringtone: {
                if (mContactData == null) return false;
                doPickRingtone();
                return true;
            }
            case R.id.menu_send_via_sms: {
                if (mContactData == null) return false;
                sendContactViaSMS();
                return true;
            }
            case R.id.menu_copy_to_phone: {
                if (mContactData == null) return false;
                copyToPhone();
                return true;
            }
            case R.id.menu_copy_to_sim1: {
                if (mContactData == null) return false;
                copyToCard(SimContactsConstants.SUB_1);
                return true;
            }
            case R.id.menu_copy_to_sim2: {
                if (mContactData == null) return false;
                copyToCard(SimContactsConstants.SUB_2);
                return true;
            }
            case R.id.menu_share: {
                if (mContactData == null) return false;

                final String lookupKey = mContactData.getLookupKey();
                Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
                if (mContactData.isUserProfile()) {
                    // User is sharing the profile.  We don't want to force the receiver to have
                    // the highly-privileged READ_PROFILE permission, so we need to request a
                    // pre-authorized URI from the provider.
                    shareUri = getPreAuthorizedUri(shareUri);
                }

                final Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType(Contacts.CONTENT_VCARD_TYPE);
                intent.putExtra(Intent.EXTRA_STREAM, shareUri);

                // Launch chooser to share contact via
                final CharSequence chooseTitle = mContext.getText(R.string.share_via);
                final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);

                try {
                    mContext.startActivity(chooseIntent);
                } catch (ActivityNotFoundException ex) {
                    Toast.makeText(mContext, R.string.share_error, Toast.LENGTH_SHORT).show();
                }
                return true;
            }
            case R.id.menu_send_to_voicemail: {
                // Update state and save
                mSendToVoicemailState = !mSendToVoicemailState;
                item.setChecked(mSendToVoicemailState);
                Intent intent = ContactSaveService.createSetSendToVoicemail(
                        mContext, mLookupUri, mSendToVoicemailState);
                mContext.startService(intent);
                return true;
            }
            case R.id.menu_create_contact_shortcut: {
                // Create a launcher shortcut with this contact
                createLauncherShortcutWithContact();
                return true;
            }
        }
        return false;
    }

    private void copyToPhone() {
        String name = mContactData.getDisplayName();
        if (TextUtils.isEmpty(name)) {
            name = "";
        }
        String phoneNumber = "";
        String anrNumber = "";
        String email = "";

        //get phonenumber,email,anr from SIM contacts,then insert them to phone
        for (RawContact rawContact : mContactData.getRawContacts()) {
            for (DataItem dataItem : rawContact.getDataItems()) {
                if (dataItem.getMimeType() == null) {
                    continue;
                }
                if (dataItem instanceof PhoneDataItem) {
                    PhoneDataItem phoneNum = (PhoneDataItem) dataItem;
                    final String number = phoneNum.getNumber();
                    if (!TextUtils.isEmpty(number)) {
                        if (Phone.TYPE_MOBILE == phoneNum.getContentValues().getAsInteger(
                                Phone.TYPE)) {
                            phoneNumber = number;
                        } else {
                            if (!anrNumber.equals("")) {
                                anrNumber += ",";
                            } else {
                                anrNumber += number;
                            }
                        }
                    }
                } else if (dataItem instanceof EmailDataItem) {
                    EmailDataItem emailData = (EmailDataItem) dataItem;
                    final String address = emailData.getData();
                    if (!TextUtils.isEmpty(address)) {
                        email = address;
                    }
                }
            }
        }

        String[] value = new String[] {
                name, phoneNumber, email, anrNumber
        };
        MoreContactUtils
                .insertToPhone(value, mContext.getContentResolver(),
                        SimContactsConstants.SUB_INVALID);
    }

    private Handler mHandler = null;

    private void copyToCard(final int sub) {
        final int MSG_COPY_DONE = 0;
        final int MSG_COPY_FAILURE = 1;
        final int MSG_CARD_NO_SPACE = 2;
        final int MSG_NO_EMPTY_EMAIL = 3;
        if (mHandler == null) {
            mHandler = new Handler() {
                public void handleMessage(Message msg) {
                    switch (msg.what) {
                        case MSG_COPY_DONE:
                            Toast.makeText(mContext, R.string.copy_done, Toast.LENGTH_SHORT)
                                    .show();
                            break;
                        case MSG_COPY_FAILURE:
                            Toast.makeText(mContext, R.string.copy_failure, Toast.LENGTH_SHORT)
                                    .show();
                            break;
                        case MSG_CARD_NO_SPACE:
                            Toast.makeText(mContext, R.string.card_no_space, Toast.LENGTH_SHORT)
                                    .show();
                            break;
                        case MSG_NO_EMPTY_EMAIL:
                            Toast.makeText(mContext, R.string.no_empty_email_in_usim,
                                    Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            };
        }

        new Thread(new Runnable() {
            public void run() {
                synchronized (this) {
                    int adnCountInSimContact = 1;
                    int anrCountInSimContact = 1;
                    int emailCountInSimContact = 0;
                    if (!MoreContactUtils.canSaveAnr(sub)) {
                        anrCountInSimContact = 0;
                    } else {
                        anrCountInSimContact = MoreContactUtils.getOneSimAnrCount(sub);
                    }
                    if (MoreContactUtils.canSaveEmail(sub)) {
                        emailCountInSimContact = MoreContactUtils.getOneSimEmailCount(sub);
                    }
                    int totalEmptyAdn = MoreContactUtils.getSimFreeCount(mContext, sub);
                    int totalEmptyAnr = MoreContactUtils.getSpareAnrCount(sub);
                    int totalEmptyEmail = MoreContactUtils.getSpareEmailCount(sub);

                    Message msg = Message.obtain();
                    if (totalEmptyAdn <= 0) {
                        msg.what = MSG_CARD_NO_SPACE;
                        mHandler.sendMessage(msg);
                        return;
                    }

                    //to indiacate how many number in one ADN can saved to SIM card,
                    //1 means can only save one number,2 means can save anr
                    int numEntitySize = adnCountInSimContact + anrCountInSimContact;

                    //empty number is equals to the sum of adn and anr
                    int emptyNumTotal = totalEmptyAdn + totalEmptyAnr;

                    // Get name string
                    String strName = mContactData.getDisplayName();

                    ArrayList<String> arrayNumber = new ArrayList<String>();
                    ArrayList<String> arrayEmail = new ArrayList<String>();

                    for (RawContact rawContact : mContactData.getRawContacts()) {
                        for (DataItem dataItem : rawContact.getDataItems()) {
                            if (dataItem.getMimeType() == null) {
                                continue;
                            }
                            if (dataItem instanceof PhoneDataItem) {
                                // Get phone string
                                PhoneDataItem phoneNum = (PhoneDataItem) dataItem;
                                final String number = phoneNum.getNumber();
                                if (!TextUtils.isEmpty(number) && emptyNumTotal-- > 0) {
                                    arrayNumber.add(number);
                                }
                            } else if (dataItem instanceof EmailDataItem) {
                                // Get email string
                                EmailDataItem emailData = (EmailDataItem) dataItem;
                                final String address = emailData.getData();
                                if (!TextUtils.isEmpty(address) && totalEmptyEmail-- > 0) {
                                    arrayEmail.add(address);
                                }
                            }
                        }
                    }

                    //calculate how many ADN needed according to the number name,phone,email,
                    //and uses the max of them
                    int nameCount = (strName != null && !strName.equals("")) ? 1 : 0;
                    int groupNumCount = (arrayNumber.size() % numEntitySize) != 0 ? (arrayNumber
                            .size() / numEntitySize + 1) : (arrayNumber.size() / numEntitySize);
                    int groupEmailCount = emailCountInSimContact == 0 ? 0
                            : ((arrayEmail.size() % emailCountInSimContact) != 0 ? (arrayEmail
                                    .size() / emailCountInSimContact + 1)
                                    : (arrayEmail.size() / emailCountInSimContact));

                    int groupCount = Math.max(groupEmailCount, Math.max(nameCount, groupNumCount));

                    ArrayList<UsimEntity> results = new ArrayList<UsimEntity>();
                    for (int i = 0; i < groupCount; i++) {
                        results.add(new UsimEntity());
                    }

                    UsimEntity value;
                    //get the phone number for each ADN from arrayNumber,put them in UsimEntity
                    for (int i = 0; i < groupNumCount; i++) {
                        value = results.get(i);
                        ArrayList<String> numberItem = new ArrayList<String>();
                        for (int j = 0; j < numEntitySize; j++) {
                            if ((i * numEntitySize + j) < arrayNumber.size()) {
                                numberItem.add(arrayNumber.get(i * numEntitySize + j));
                            }
                        }
                        value.putNumberList(numberItem);
                    }

                    for (int i = 0; i < groupEmailCount; i++) {
                        value = results.get(i);
                        ArrayList<String> emailItem = new ArrayList<String>();
                        for (int j = 0; j < emailCountInSimContact; j++) {
                            if ((i * emailCountInSimContact + j) < arrayEmail.size()) {
                                emailItem.add(arrayEmail.get(i * emailCountInSimContact + j));
                            }
                        }
                        value.putEmailList(emailItem);
                    }

                    ArrayList<String> emptyList = new ArrayList<String>();
                    Uri itemUri = null;
                    if (totalEmptyEmail < 0 && MoreContactUtils.canSaveEmail(sub)) {
                        Message e_msg = Message.obtain();
                        e_msg.what = MSG_NO_EMPTY_EMAIL;
                        mHandler.sendMessage(e_msg);
                    }

                    //get phone number from UsimEntity,then insert to SIM card
                    for (int i = 0; i < groupCount; i++) {
                        value = results.get(i);
                        if (value.containsNumber()) {
                            arrayNumber = (ArrayList<String>) value.getNumberList();
                        } else {
                            arrayNumber = emptyList;
                        }

                        if (value.containsEmail()) {
                            arrayEmail = (ArrayList<String>) value.getEmailList();
                        } else {
                            arrayEmail = emptyList;
                        }
                        String strNum = arrayNumber.size() > 0 ? arrayNumber.get(0) : null;
                        StringBuilder strAnrNum = new StringBuilder();
                        for (int j = 1; j < arrayNumber.size(); j++) {
                            String s = arrayNumber.get(j);
                            if (s.length() > MoreContactUtils.MAX_LENGTH_NUMBER_IN_SIM) {
                                s = s.substring(
                                        0, MoreContactUtils.MAX_LENGTH_NUMBER_IN_SIM);
                            }
                            strAnrNum.append(s);
                            strAnrNum.append(",");
                        }
                        StringBuilder strEmail = new StringBuilder();
                        for (int j = 0; j < arrayEmail.size(); j++) {
                            String s = arrayEmail.get(j);
                            if (s.length() > MoreContactUtils.MAX_LENGTH_EMAIL_IN_SIM) {
                                s = s.substring(
                                        0, MoreContactUtils.MAX_LENGTH_EMAIL_IN_SIM);
                            }
                            strEmail.append(s);
                            strEmail.append(",");
                        }
                        itemUri = MoreContactUtils.insertToCard(mContext, strName, strNum,
                                strEmail.toString(), strAnrNum.toString(), sub);
                    }
                    if (itemUri != null) {
                        msg.what = MSG_COPY_DONE;
                        mHandler.sendMessage(msg);
                    } else {
                        msg.what = MSG_COPY_FAILURE;
                        mHandler.sendMessage(msg);
                    }
                }
            }
        }).start();

    }

    /**
     * Creates a launcher shortcut with the current contact.
     */
    private void createLauncherShortcutWithContact() {
        // Hold the parent activity of this fragment in case this fragment is destroyed
        // before the callback to onShortcutIntentCreated(...)
        final Activity parentActivity = getActivity();

        ShortcutIntentBuilder builder = new ShortcutIntentBuilder(parentActivity,
                new OnShortcutIntentCreatedListener() {

            @Override
            public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
                // Broadcast the shortcutIntent to the launcher to create a
                // shortcut to this contact
                shortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
                parentActivity.sendBroadcast(shortcutIntent);
            }

        });
        builder.createContactShortcutIntent(mLookupUri);
    }

    /**
     * Calls into the contacts provider to get a pre-authorized version of the given URI.
     */
    private Uri getPreAuthorizedUri(Uri uri) {
        Bundle uriBundle = new Bundle();
        uriBundle.putParcelable(ContactsContract.Authorization.KEY_URI_TO_AUTHORIZE, uri);
        Bundle authResponse = mContext.getContentResolver().call(
                ContactsContract.AUTHORITY_URI,
                ContactsContract.Authorization.AUTHORIZATION_METHOD,
                null,
                uriBundle);
        if (authResponse != null) {
            return (Uri) authResponse.getParcelable(
                    ContactsContract.Authorization.KEY_AUTHORIZED_URI);
        } else {
            return uri;
        }
    }

    @Override
    public boolean handleKeyDown(int keyCode) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DEL: {
                if (mListener != null) mListener.onDeleteRequested(mLookupUri);
                return true;
            }
        }
        return false;
    }

    private void sendContactViaSMS() {
        // Get name string
        String name = mContactData.getDisplayName();
        String phone = null;
        String email = null;
        String postal = null;
        String organization = null;
        String sipAddress = null;

        Log.d(TAG, "Contact name: " + name);

        for (RawContact raw: mContactData.getRawContacts()) {
            final ContentValues entValues = raw.getValues();
            Log.d(TAG, "  entValues:" + entValues);

            for (RawContact.NamedDataItem namedDataItem : raw.getNamedDataItems()) {
                final ContentValues entryValues = namedDataItem.mContentValues;
                final String mimeType = entryValues.getAsString(Data.MIMETYPE);

                Log.d(TAG, "    entryValues:" + entryValues);

                if (mimeType == null) continue;

                if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)) { // Get phone string
                    if (phone == null) {
                        phone = entryValues.getAsString(Phone.NUMBER);
                    } else {
                        phone = phone + ", " + entryValues.getAsString(Phone.NUMBER);
                    }
                } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) { // Get email string
                    if (email == null) {
                        email = entryValues.getAsString(Email.ADDRESS);
                    } else {
                        email = email + ", " + entryValues.getAsString(Email.ADDRESS);
                    }
                } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType)) {
                    if (postal == null) {
                        postal = entryValues.getAsString(StructuredPostal.FORMATTED_ADDRESS);
                    } else {
                        postal = postal + ", " + entryValues.getAsString(
                                 StructuredPostal.FORMATTED_ADDRESS);
                    }
                } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType)) {
                    if (organization == null) {
                        organization = entryValues.getAsString(Organization.COMPANY);
                    } else {
                        organization = organization + ", " + entryValues
                                     .getAsString(Organization.COMPANY);
                    }
                } else if (SipAddress.CONTENT_ITEM_TYPE.equals(mimeType)) {
                    if (sipAddress == null) {
                        sipAddress = entryValues.getAsString(SipAddress.SIP_ADDRESS);
                    } else {
                        sipAddress = sipAddress + ", " + entryValues
                                    .getAsString(SipAddress.SIP_ADDRESS);
                    }
                }
            }
        }

        if (TextUtils.isEmpty(name)) {
            name = mContext.getResources().getString(R.string.missing_name);
        }

        name = getString(R.string.nameLabelsGroup) + ":" + name + "\r\n";
        phone = (phone == null) ? "" : getString(R.string.phoneLabelsGroup)
                + ":" + phone + "\r\n";
        email = (email == null )? "" : getString(R.string.emailLabelsGroup)
                + ":" + email + "\r\n";
        postal = (postal == null) ? "" : getString(R.string.postalLabelsGroup)
                + ":" + postal + "\r\n";
        organization = (organization == null) ? "" : getString(R.string.organizationLabelsGroup)
                + ":" + organization + "\r\n";
        sipAddress = (sipAddress == null) ? "" : getString(R.string.label_sip_address) + ":"
                + sipAddress + "\r\n";
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.putExtra("sms_body", name + phone + email + postal + organization + sipAddress);
        intent.setType("vnd.android-dir/mms-sms");
        try {
            mContext.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(mContext, R.string.share_error, Toast.LENGTH_SHORT).show();
        }
    }

    //supply phone number and email which could stored in one ADN
    class UsimEntity {
        private ArrayList<String> mNumberList = new ArrayList<String>();
        private ArrayList<String> mEmailList = new ArrayList<String>();

        public ArrayList<String> getEmailList() {
            return mEmailList;
        }

        public ArrayList<String> getNumberList() {
            return mNumberList;
        }

        public void putEmailList(ArrayList<String> list) {
            mEmailList = list;
        }

        public void putNumberList(ArrayList<String> list) {
            mNumberList = list;
        }

        public boolean containsEmail() {
            return !mEmailList.isEmpty();
        }

        public boolean containsNumber() {
            return !mNumberList.isEmpty();
        }
    }

    private void doPickRingtone() {

        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        // Allow user to pick 'Default'
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
        // Show only ringtones
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
        // Allow the user to pick a silent ringtone
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
        // Set HoloLight theme dialog
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DIALOG_THEME, R.style.Theme_RingtoneDialog);

        Uri ringtoneUri;
        if (mCustomRingtone != null) {
            ringtoneUri = Uri.parse(mCustomRingtone);
        } else {
            // Otherwise pick default ringtone Uri so that something is selected.
            ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        }

        // Put checkmark next to the current ringtone for this contact
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtoneUri);

        // Launch!
        startActivityForResult(intent, REQUEST_CODE_PICK_RINGTONE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }

        switch (requestCode) {
            case REQUEST_CODE_PICK_RINGTONE: {
                Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                handleRingtonePicked(pickedUri);
                break;
            }
        }
    }

    private void handleRingtonePicked(Uri pickedUri) {
        if (pickedUri == null || RingtoneManager.isDefault(pickedUri)) {
            mCustomRingtone = null;
        } else {
            mCustomRingtone = pickedUri.toString();
        }
        Intent intent = ContactSaveService.createSetRingtone(
                mContext, mLookupUri, mCustomRingtone);
        mContext.startService(intent);
    }
}