19c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng/*
29c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng * Copyright (C) 2010 The Android Open Source Project
39c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng *
49c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
59c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng * you may not use this file except in compliance with the License.
69c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng * You may obtain a copy of the License at
79c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng *
89c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
99c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng *
109c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng * Unless required by applicable law or agreed to in writing, software
119c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
129c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng * See the License for the specific language governing permissions and
149c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng * limitations under the License.
159c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng */
169c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengpackage com.android.dialer.interactions;
179c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
189c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.app.Activity;
199c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.app.AlertDialog;
209c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.app.Dialog;
219c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.app.DialogFragment;
229c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.app.FragmentManager;
239c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.content.Context;
249c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.content.CursorLoader;
259c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.content.DialogInterface;
269c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.content.DialogInterface.OnDismissListener;
279c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.content.Intent;
289c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.content.Loader;
299c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.content.Loader.OnLoadCompleteListener;
309c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.database.Cursor;
319c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.net.Uri;
329c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.os.Bundle;
339c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.os.Parcel;
349c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.os.Parcelable;
359c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.provider.ContactsContract.CommonDataKinds.Phone;
369c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.provider.ContactsContract.CommonDataKinds.SipAddress;
379c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.provider.ContactsContract.Contacts;
389c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.provider.ContactsContract.Data;
399c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.provider.ContactsContract.RawContacts;
409c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.view.LayoutInflater;
419c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.view.View;
429c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.view.ViewGroup;
439c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.widget.ArrayAdapter;
449c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.widget.CheckBox;
459c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.widget.ListAdapter;
469c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport android.widget.TextView;
479c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
489c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport com.android.contacts.common.CallUtil;
499c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport com.android.contacts.common.Collapser;
509c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport com.android.contacts.common.Collapser.Collapsible;
519c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport com.android.contacts.common.MoreContactUtils;
529c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport com.android.contacts.common.activity.TransactionSafeActivity;
539c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport com.android.contacts.common.util.ContactDisplayUtils;
549c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport com.android.dialer.R;
559c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport com.android.dialer.contact.ContactUpdateService;
569c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport com.google.common.annotations.VisibleForTesting;
579c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
589c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport java.util.ArrayList;
599c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengimport java.util.List;
609c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
619c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng/**
629c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng * Initiates phone calls or a text message. If there are multiple candidates, this class shows a
639c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng * dialog to pick one. Creating one of these interactions should be done through the static
649c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng * factory methods.
659c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng *
669c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng * Note that this class initiates not only usual *phone* calls but also *SIP* calls.
679c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng *
689c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng * TODO: clean up code and documents since it is quite confusing to use "phone numbers" or
699c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng *        "phone calls" here while they can be SIP addresses or SIP calls (See also issue 5039627).
709c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng */
719c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Chengpublic class PhoneNumberInteraction implements OnLoadCompleteListener<Cursor> {
729c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    private static final String TAG = PhoneNumberInteraction.class.getSimpleName();
739c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
749c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    /**
759c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * A model object for capturing a phone number for a given contact.
769c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     */
779c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    @VisibleForTesting
789c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    /* package */ static class PhoneItem implements Parcelable, Collapsible<PhoneItem> {
799c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        long id;
809c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        String phoneNumber;
819c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        String accountType;
829c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        String dataSet;
839c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        long type;
849c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        String label;
859c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        /** {@link Phone#CONTENT_ITEM_TYPE} or {@link SipAddress#CONTENT_ITEM_TYPE}. */
869c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        String mimeType;
879c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
889c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        public PhoneItem() {
899c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
909c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
919c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        private PhoneItem(Parcel in) {
929c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            this.id          = in.readLong();
939c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            this.phoneNumber = in.readString();
949c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            this.accountType = in.readString();
959c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            this.dataSet     = in.readString();
969c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            this.type        = in.readLong();
979c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            this.label       = in.readString();
989c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            this.mimeType    = in.readString();
999c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
1009c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1019c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        @Override
1029c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        public void writeToParcel(Parcel dest, int flags) {
1039c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            dest.writeLong(id);
1049c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            dest.writeString(phoneNumber);
1059c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            dest.writeString(accountType);
1069c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            dest.writeString(dataSet);
1079c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            dest.writeLong(type);
1089c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            dest.writeString(label);
1099c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            dest.writeString(mimeType);
1109c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
1119c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1129c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        @Override
1139c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        public int describeContents() {
1149c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            return 0;
1159c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
1169c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1179c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        @Override
118c36c492a94bf4321a2dfcd94be0b5bfe773347c9Jay Shrauner        public void collapseWith(PhoneItem phoneItem) {
1199c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            // Just keep the number and id we already have.
1209c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
1219c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1229c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        @Override
1239c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        public boolean shouldCollapseWith(PhoneItem phoneItem) {
1249c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            return MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, phoneNumber,
1259c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                    Phone.CONTENT_ITEM_TYPE, phoneItem.phoneNumber);
1269c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
1279c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1289c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        @Override
1299c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        public String toString() {
1309c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            return phoneNumber;
1319c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
1329c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1339c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        public static final Parcelable.Creator<PhoneItem> CREATOR
1349c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                = new Parcelable.Creator<PhoneItem>() {
1359c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            @Override
1369c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            public PhoneItem createFromParcel(Parcel in) {
1379c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                return new PhoneItem(in);
1389c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            }
1399c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1409c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            @Override
1419c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            public PhoneItem[] newArray(int size) {
1429c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                return new PhoneItem[size];
1439c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            }
1449c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        };
1459c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
1469c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1479c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    /**
1489c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * A list adapter that populates the list of contact's phone numbers.
1499c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     */
1509c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    private static class PhoneItemAdapter extends ArrayAdapter<PhoneItem> {
1519c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        private final int mInteractionType;
1529c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1539c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        public PhoneItemAdapter(Context context, List<PhoneItem> list,
1549c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                int interactionType) {
1559c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            super(context, R.layout.phone_disambig_item, android.R.id.text2, list);
1569c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            mInteractionType = interactionType;
1579c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
1589c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1599c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        @Override
1609c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        public View getView(int position, View convertView, ViewGroup parent) {
1619c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            final View view = super.getView(position, convertView, parent);
1629c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1639c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            final PhoneItem item = getItem(position);
1649c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            final TextView typeView = (TextView) view.findViewById(android.R.id.text1);
1659c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            CharSequence value = ContactDisplayUtils.getLabelForCallOrSms((int) item.type,
1669c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                    item.label, mInteractionType, getContext());
1679c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1689c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            typeView.setText(value);
1699c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            return view;
1709c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
1719c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
1729c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1739c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    /**
1749c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * {@link DialogFragment} used for displaying a dialog with a list of phone numbers of which
1759c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * one will be chosen to make a call or initiate an sms message.
1769c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     *
1779c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * It is recommended to use
1789c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * {@link PhoneNumberInteraction#startInteractionForPhoneCall(TransactionSafeActivity, Uri)} or
1799c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * {@link PhoneNumberInteraction#startInteractionForTextMessage(TransactionSafeActivity, Uri)}
1809c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * instead of directly using this class, as those methods handle one or multiple data cases
1819c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * appropriately.
1829c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     */
1839c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    /* Made public to let the system reach this class */
1849c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    public static class PhoneDisambiguationDialogFragment extends DialogFragment
1859c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
1869c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1879c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        private static final String ARG_PHONE_LIST = "phoneList";
1889c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        private static final String ARG_INTERACTION_TYPE = "interactionType";
1899c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        private static final String ARG_CALL_ORIGIN = "callOrigin";
1909c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1919c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        private int mInteractionType;
1929c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        private ListAdapter mPhonesAdapter;
1939c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        private List<PhoneItem> mPhoneList;
1949c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        private String mCallOrigin;
1959c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
1969c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        public static void show(FragmentManager fragmentManager,
1979c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                ArrayList<PhoneItem> phoneList, int interactionType,
1989c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                String callOrigin) {
1999c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            PhoneDisambiguationDialogFragment fragment = new PhoneDisambiguationDialogFragment();
2009c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            Bundle bundle = new Bundle();
2019c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            bundle.putParcelableArrayList(ARG_PHONE_LIST, phoneList);
2029c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            bundle.putSerializable(ARG_INTERACTION_TYPE, interactionType);
2039c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            bundle.putString(ARG_CALL_ORIGIN, callOrigin);
2049c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            fragment.setArguments(bundle);
2059c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            fragment.show(fragmentManager, TAG);
2069c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
2079c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
2089c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        @Override
2099c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        public Dialog onCreateDialog(Bundle savedInstanceState) {
2109c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            final Activity activity = getActivity();
2119c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            mPhoneList = getArguments().getParcelableArrayList(ARG_PHONE_LIST);
2129c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            mInteractionType = getArguments().getInt(ARG_INTERACTION_TYPE);
2139c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            mCallOrigin = getArguments().getString(ARG_CALL_ORIGIN);
2149c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
2159c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            mPhonesAdapter = new PhoneItemAdapter(activity, mPhoneList, mInteractionType);
2169c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            final LayoutInflater inflater = activity.getLayoutInflater();
2179c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            final View setPrimaryView = inflater.inflate(R.layout.set_primary_checkbox, null);
2189c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            return new AlertDialog.Builder(activity)
2199c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                    .setAdapter(mPhonesAdapter, this)
2209c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                    .setTitle(mInteractionType == ContactDisplayUtils.INTERACTION_SMS
2219c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                            ? R.string.sms_disambig_title : R.string.call_disambig_title)
2229c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                    .setView(setPrimaryView)
2239c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                    .create();
2249c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
2259c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
2269c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        @Override
2279c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        public void onClick(DialogInterface dialog, int which) {
2289c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            final Activity activity = getActivity();
2299c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            if (activity == null) return;
2309c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            final AlertDialog alertDialog = (AlertDialog)dialog;
2319c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            if (mPhoneList.size() > which && which >= 0) {
2329c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                final PhoneItem phoneItem = mPhoneList.get(which);
2339c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                final CheckBox checkBox = (CheckBox)alertDialog.findViewById(R.id.setPrimary);
2349c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                if (checkBox.isChecked()) {
2359c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                    // Request to mark the data as primary in the background.
2369c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                    final Intent serviceIntent = ContactUpdateService.createSetSuperPrimaryIntent(
2379c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                            activity, phoneItem.id);
2389c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                    activity.startService(serviceIntent);
2399c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                }
2409c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
2419c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                PhoneNumberInteraction.performAction(activity, phoneItem.phoneNumber,
2429c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                        mInteractionType, mCallOrigin);
2439c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            } else {
2449c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                dialog.dismiss();
2459c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            }
2469c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
2479c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
2489c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
2499c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    private static final String[] PHONE_NUMBER_PROJECTION = new String[] {
2509c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            Phone._ID,
2519c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            Phone.NUMBER,
2529c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            Phone.IS_SUPER_PRIMARY,
2539c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            RawContacts.ACCOUNT_TYPE,
2549c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            RawContacts.DATA_SET,
2559c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            Phone.TYPE,
2569c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            Phone.LABEL,
2579c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            Phone.MIMETYPE
2589c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    };
2599c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
2609c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    private static final String PHONE_NUMBER_SELECTION =
2619c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            Data.MIMETYPE + " IN ('"
2629c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                + Phone.CONTENT_ITEM_TYPE + "', "
2639c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                + "'" + SipAddress.CONTENT_ITEM_TYPE + "') AND "
2649c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                + Data.DATA1 + " NOT NULL";
2659c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
2669c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    private final Context mContext;
2679c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    private final OnDismissListener mDismissListener;
2689c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    private final int mInteractionType;
2699c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
2709c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    private final String mCallOrigin;
27155b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee    private boolean mUseDefault;
2729c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
2739c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    private CursorLoader mLoader;
2749c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
2759c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    /**
2769c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * Constructs a new {@link PhoneNumberInteraction}. The constructor takes in a {@link Context}
2779c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * instead of a {@link TransactionSafeActivity} for testing purposes to verify the functionality
2789c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * of this class. However, all factory methods for creating {@link PhoneNumberInteraction}s
2799c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * require a {@link TransactionSafeActivity} (i.e. see {@link #startInteractionForPhoneCall}).
2809c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     */
2819c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    @VisibleForTesting
2829c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    /* package */ PhoneNumberInteraction(Context context, int interactionType,
2839c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            DialogInterface.OnDismissListener dismissListener) {
2849c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        this(context, interactionType, dismissListener, null);
2859c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
2869c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
2879c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    private PhoneNumberInteraction(Context context, int interactionType,
2889c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            DialogInterface.OnDismissListener dismissListener, String callOrigin) {
2899c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        mContext = context;
2909c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        mInteractionType = interactionType;
2919c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        mDismissListener = dismissListener;
2929c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        mCallOrigin = callOrigin;
2939c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
2949c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
2959c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    private void performAction(String phoneNumber) {
2969c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        PhoneNumberInteraction.performAction(mContext, phoneNumber, mInteractionType, mCallOrigin);
2979c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
2989c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
2999c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    private static void performAction(
3009c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            Context context, String phoneNumber, int interactionType,
3019c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            String callOrigin) {
3029c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        Intent intent;
3039c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        switch (interactionType) {
3049c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            case ContactDisplayUtils.INTERACTION_SMS:
3059c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                intent = new Intent(
3069c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                        Intent.ACTION_SENDTO, Uri.fromParts("sms", phoneNumber, null));
3079c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                break;
3089c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            default:
3099c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                intent = CallUtil.getCallIntent(phoneNumber, callOrigin);
3109c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                break;
3119c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
3129c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        context.startActivity(intent);
3139c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
3149c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
3159c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    /**
3169c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * Initiates the interaction. This may result in a phone call or sms message started
31755b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * or a disambiguation dialog to determine which phone number should be used. If there
31855b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * is a primary phone number, it will be automatically used and a disambiguation dialog
31955b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * will no be shown.
3209c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     */
3219c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    @VisibleForTesting
3229c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    /* package */ void startInteraction(Uri uri) {
32355b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee        startInteraction(uri, true);
32455b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee    }
32555b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee
32655b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee    /**
32755b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * Initiates the interaction to result in either a phone call or sms message for a contact.
32855b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * @param uri Contact Uri
32955b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * @param useDefault Whether or not to use the primary(default) phone number. If true, the
33055b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * primary phone number will always be used by default if one is available. If false, a
33155b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * disambiguation dialog will be shown regardless of whether or not a primary phone number
33255b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * is available.
33355b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     */
33455b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee    @VisibleForTesting
33555b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee    /* package */ void startInteraction(Uri uri, boolean useDefault) {
3369c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        if (mLoader != null) {
3379c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            mLoader.reset();
3389c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
33955b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee        mUseDefault = useDefault;
3409c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        final Uri queryUri;
3419c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        final String inputUriAsString = uri.toString();
3429c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        if (inputUriAsString.startsWith(Contacts.CONTENT_URI.toString())) {
3439c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            if (!inputUriAsString.endsWith(Contacts.Data.CONTENT_DIRECTORY)) {
3449c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                queryUri = Uri.withAppendedPath(uri, Contacts.Data.CONTENT_DIRECTORY);
3459c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            } else {
3469c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                queryUri = uri;
3479c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            }
3489c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        } else if (inputUriAsString.startsWith(Data.CONTENT_URI.toString())) {
3499c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            queryUri = uri;
3509c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        } else {
3519c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            throw new UnsupportedOperationException(
3529c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                    "Input Uri must be contact Uri or data Uri (input: \"" + uri + "\")");
3539c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
3549c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
3559c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        mLoader = new CursorLoader(mContext,
3569c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                queryUri,
3579c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                PHONE_NUMBER_PROJECTION,
3589c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                PHONE_NUMBER_SELECTION,
3599c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                null,
3609c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                null);
3619c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        mLoader.registerListener(0, this);
3629c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        mLoader.startLoading();
3639c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
3649c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
3659c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    @Override
3669c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    public void onLoadComplete(Loader<Cursor> loader, Cursor cursor) {
3679c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        if (cursor == null || !isSafeToCommitTransactions()) {
3689c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            onDismiss();
3699c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            return;
3709c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
3719c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        ArrayList<PhoneItem> phoneList = new ArrayList<PhoneItem>();
3729c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        String primaryPhone = null;
3739c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        try {
3749c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            while (cursor.moveToNext()) {
37555b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee                if (mUseDefault && cursor.getInt(cursor.getColumnIndex(Phone.IS_SUPER_PRIMARY)) != 0) {
3769c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                    // Found super primary, call it.
3779c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                    primaryPhone = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
3789c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                }
3799c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
3809c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                PhoneItem item = new PhoneItem();
3819c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                item.id = cursor.getLong(cursor.getColumnIndex(Data._ID));
3829c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                item.phoneNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
3839c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                item.accountType =
3849c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                        cursor.getString(cursor.getColumnIndex(RawContacts.ACCOUNT_TYPE));
3859c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                item.dataSet = cursor.getString(cursor.getColumnIndex(RawContacts.DATA_SET));
3869c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                item.type = cursor.getInt(cursor.getColumnIndex(Phone.TYPE));
3879c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                item.label = cursor.getString(cursor.getColumnIndex(Phone.LABEL));
3889c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                item.mimeType = cursor.getString(cursor.getColumnIndex(Phone.MIMETYPE));
3899c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
3909c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                phoneList.add(item);
3919c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            }
3929c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        } finally {
3939c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            cursor.close();
3949c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
3959c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
39655b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee        if (mUseDefault && primaryPhone != null) {
3979c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            performAction(primaryPhone);
3989c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            onDismiss();
3999c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            return;
4009c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
4019c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
4029c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        Collapser.collapseList(phoneList);
4039c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
4049c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        if (phoneList.size() == 0) {
4059c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            onDismiss();
4069c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        } else if (phoneList.size() == 1) {
4079c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            PhoneItem item = phoneList.get(0);
4089c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            onDismiss();
4099c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            performAction(item.phoneNumber);
4109c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        } else {
4119c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            // There are multiple candidates. Let the user choose one.
4129c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            showDisambiguationDialog(phoneList);
4139c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
4149c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
4159c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
4169c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    private boolean isSafeToCommitTransactions() {
4179c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        return mContext instanceof TransactionSafeActivity ?
4189c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                ((TransactionSafeActivity) mContext).isSafeToCommitTransactions() : true;
4199c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
4209c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
4219c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    private void onDismiss() {
4229c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        if (mDismissListener != null) {
4239c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            mDismissListener.onDismiss(null);
4249c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        }
4259c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
4269c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
4279c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    /**
4289c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * Start call action using given contact Uri. If there are multiple candidates for the phone
4299c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * call, dialog is automatically shown and the user is asked to choose one.
4309c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     *
4319c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * @param activity that is calling this interaction. This must be of type
4329c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * {@link TransactionSafeActivity} because we need to check on the activity state after the
4339c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * phone numbers have been queried for.
4349c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * @param uri contact Uri (built from {@link Contacts#CONTENT_URI}) or data Uri
4359c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * (built from {@link Data#CONTENT_URI}). Contact Uri may show the disambiguation dialog while
4369c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * data Uri won't.
4379c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     */
4389c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    public static void startInteractionForPhoneCall(TransactionSafeActivity activity, Uri uri) {
4399c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        (new PhoneNumberInteraction(activity, ContactDisplayUtils.INTERACTION_CALL, null))
44055b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee                .startInteraction(uri, true);
44155b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee    }
44255b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee
44355b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee    /**
44455b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * Start call action using given contact Uri. If there are multiple candidates for the phone
44555b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * call, dialog is automatically shown and the user is asked to choose one.
44655b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     *
44755b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * @param activity that is calling this interaction. This must be of type
44855b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * {@link TransactionSafeActivity} because we need to check on the activity state after the
44955b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * phone numbers have been queried for.
45055b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * @param uri contact Uri (built from {@link Contacts#CONTENT_URI}) or data Uri
45155b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * (built from {@link Data#CONTENT_URI}). Contact Uri may show the disambiguation dialog while
45255b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * data Uri won't.
45355b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * @param useDefault Whether or not to use the primary(default) phone number. If true, the
45455b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * primary phone number will always be used by default if one is available. If false, a
45555b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * disambiguation dialog will be shown regardless of whether or not a primary phone number
45655b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     * is available.
45755b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee     */
45855b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee    public static void startInteractionForPhoneCall(TransactionSafeActivity activity, Uri uri,
45955b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee            boolean useDefault) {
46055b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee        (new PhoneNumberInteraction(activity, ContactDisplayUtils.INTERACTION_CALL, null))
46155b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee                .startInteraction(uri, useDefault);
4629c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
4639c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
4649c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    /**
4659c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * @param activity that is calling this interaction. This must be of type
4669c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * {@link TransactionSafeActivity} because we need to check on the activity state after the
4679c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * phone numbers have been queried for.
4689c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * @param callOrigin If non null, {@link PhoneConstants#EXTRA_CALL_ORIGIN} will be
4699c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * appended to the Intent initiating phone call. See comments in Phone package (PhoneApp)
4709c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * for more detail.
4719c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     */
4729c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    public static void startInteractionForPhoneCall(TransactionSafeActivity activity, Uri uri,
4739c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng            String callOrigin) {
4749c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        (new PhoneNumberInteraction(activity, ContactDisplayUtils.INTERACTION_CALL, null, callOrigin))
47555b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee                .startInteraction(uri, true);
4769c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
4779c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
4789c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    /**
4799c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * Start text messaging (a.k.a SMS) action using given contact Uri. If there are multiple
4809c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * candidates for the phone call, dialog is automatically shown and the user is asked to choose
4819c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * one.
4829c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     *
4839c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * @param activity that is calling this interaction. This must be of type
4849c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * {@link TransactionSafeActivity} because we need to check on the activity state after the
4859c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * phone numbers have been queried for.
4869c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * @param uri contact Uri (built from {@link Contacts#CONTENT_URI}) or data Uri
4879c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * (built from {@link Data#CONTENT_URI}). Contact Uri may show the disambiguation dialog while
4889c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     * data Uri won't.
4899c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng     */
4909c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    public static void startInteractionForTextMessage(TransactionSafeActivity activity, Uri uri) {
4919c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        (new PhoneNumberInteraction(activity, ContactDisplayUtils.INTERACTION_SMS, null))
49255b030a8bbb0323a4823fc34f3167a248a0ff899Yorke Lee                .startInteraction(uri, true);
4939c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
4949c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
4959c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    @VisibleForTesting
4969c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    /* package */ CursorLoader getLoader() {
4979c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        return mLoader;
4989c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
4999c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng
5009c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    @VisibleForTesting
5019c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    /* package */ void showDisambiguationDialog(ArrayList<PhoneItem> phoneList) {
5029c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng        PhoneDisambiguationDialogFragment.show(((Activity)mContext).getFragmentManager(),
5039c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng                phoneList, mInteractionType, mCallOrigin);
5049c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng    }
5059c5fe4420bc1a1fe4edcac99e1f73b13bca63fa2Chiao Cheng}
506