blob: 3b32b5769ec9d89694ae68cc444b88ea58bf3df4 [file] [log] [blame]
Evan Millar14fecb62009-09-09 09:23:12 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.contacts;
18
Evan Millaradb0f8c2009-09-28 17:20:50 -070019import com.android.contacts.Collapser.Collapsible;
Dmitri Plotnikov3f5686e2010-03-05 10:30:03 -080020import com.android.contacts.model.ContactsSource;
21import com.android.contacts.model.Sources;
22import com.android.contacts.model.ContactsSource.DataKind;
23import com.android.contacts.model.ContactsSource.StringInflater;
Evan Millar14fecb62009-09-09 09:23:12 -070024
25import android.app.AlertDialog;
26import android.content.ContentUris;
27import android.content.ContentValues;
28import android.content.Context;
29import android.content.DialogInterface;
30import android.database.Cursor;
31import android.provider.ContactsContract.Data;
Dmitri Plotnikov3f5686e2010-03-05 10:30:03 -080032import android.provider.ContactsContract.RawContacts;
Evan Millar14fecb62009-09-09 09:23:12 -070033import android.provider.ContactsContract.CommonDataKinds.Phone;
Evan Millaradb0f8c2009-09-28 17:20:50 -070034import android.telephony.PhoneNumberUtils;
Evan Millar14fecb62009-09-09 09:23:12 -070035import android.view.LayoutInflater;
36import android.view.View;
Dmitri Plotnikov3f5686e2010-03-05 10:30:03 -080037import android.view.ViewGroup;
Evan Millar14fecb62009-09-09 09:23:12 -070038import android.widget.ArrayAdapter;
39import android.widget.CheckBox;
40import android.widget.CompoundButton;
Evan Millaradb0f8c2009-09-28 17:20:50 -070041import android.widget.ListAdapter;
Dmitri Plotnikov3f5686e2010-03-05 10:30:03 -080042import android.widget.TextView;
43
44import java.util.ArrayList;
45import java.util.List;
Evan Millar14fecb62009-09-09 09:23:12 -070046
47/**
48 * Class used for displaying a dialog with a list of phone numbers of which
49 * one will be chosen to make a call or initiate an sms message.
50 */
51public class PhoneDisambigDialog implements DialogInterface.OnClickListener,
Dmitri Plotnikov7c5286e2010-05-04 16:49:36 -070052 CompoundButton.OnCheckedChangeListener{
Evan Millar14fecb62009-09-09 09:23:12 -070053
54 private boolean mMakePrimary = false;
55 private Context mContext;
56 private AlertDialog mDialog;
57 private boolean mSendSms;
Evan Millaradb0f8c2009-09-28 17:20:50 -070058 private ListAdapter mPhonesAdapter;
59 private ArrayList<PhoneItem> mPhoneItemList;
Evan Millar14fecb62009-09-09 09:23:12 -070060
61 public PhoneDisambigDialog(Context context, Cursor phonesCursor) {
62 this(context, phonesCursor, false /*make call*/);
63 }
64
65 public PhoneDisambigDialog(Context context, Cursor phonesCursor, boolean sendSms) {
66 mContext = context;
67 mSendSms = sendSms;
Evan Millaradb0f8c2009-09-28 17:20:50 -070068 mPhoneItemList = makePhoneItemsList(phonesCursor);
Dmitri Plotnikov7c5286e2010-05-04 16:49:36 -070069 phonesCursor.close();
70
Evan Millaradb0f8c2009-09-28 17:20:50 -070071 Collapser.collapseList(mPhoneItemList);
72
Dmitri Plotnikov3f5686e2010-03-05 10:30:03 -080073 mPhonesAdapter = new PhonesAdapter(mContext, mPhoneItemList, mSendSms);
Evan Millaradb0f8c2009-09-28 17:20:50 -070074
Evan Millar14fecb62009-09-09 09:23:12 -070075 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
76 Context.LAYOUT_INFLATER_SERVICE);
77 View setPrimaryView = inflater.
78 inflate(R.layout.set_primary_checkbox, null);
79 ((CheckBox) setPrimaryView.findViewById(R.id.setPrimary)).
80 setOnCheckedChangeListener(this);
81
82 // Need to show disambig dialogue.
83 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext).
Evan Millaradb0f8c2009-09-28 17:20:50 -070084 setAdapter(mPhonesAdapter, this).
85 setTitle(sendSms ?
86 R.string.sms_disambig_title : R.string.call_disambig_title).
87 setView(setPrimaryView);
Evan Millar14fecb62009-09-09 09:23:12 -070088
89 mDialog = dialogBuilder.create();
90 }
91
Dmitri Plotnikov884e3932010-05-13 19:13:51 -070092 public void setOnDismissListener(DialogInterface.OnDismissListener dismissListener) {
93 mDialog.setOnDismissListener(dismissListener);
94 }
95
Evan Millar14fecb62009-09-09 09:23:12 -070096 /**
97 * Show the dialog.
98 */
99 public void show() {
Evan Millaradb0f8c2009-09-28 17:20:50 -0700100 if (mPhoneItemList.size() == 1) {
101 // If there is only one after collapse, just select it, and close;
102 onClick(mDialog, 0);
103 return;
104 }
Evan Millar14fecb62009-09-09 09:23:12 -0700105 mDialog.show();
106 }
107
108 public void onClick(DialogInterface dialog, int which) {
Evan Millaradb0f8c2009-09-28 17:20:50 -0700109 if (mPhoneItemList.size() > which && which >= 0) {
110 PhoneItem phoneItem = mPhoneItemList.get(which);
111 long id = phoneItem.id;
112 String phone = phoneItem.phoneNumber;
113
Evan Millar14fecb62009-09-09 09:23:12 -0700114 if (mMakePrimary) {
115 ContentValues values = new ContentValues(1);
116 values.put(Data.IS_SUPER_PRIMARY, 1);
Evan Millaradb0f8c2009-09-28 17:20:50 -0700117 mContext.getContentResolver().update(ContentUris.
118 withAppendedId(Data.CONTENT_URI, id), values, null, null);
Evan Millar14fecb62009-09-09 09:23:12 -0700119 }
120
121 if (mSendSms) {
122 ContactsUtils.initiateSms(mContext, phone);
123 } else {
124 ContactsUtils.initiateCall(mContext, phone);
125 }
126 } else {
127 dialog.dismiss();
128 }
129 }
130
131 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
132 mMakePrimary = isChecked;
133 }
134
Evan Millaradb0f8c2009-09-28 17:20:50 -0700135 private static class PhonesAdapter extends ArrayAdapter<PhoneItem> {
Dmitri Plotnikov3f5686e2010-03-05 10:30:03 -0800136 private final boolean sendSms;
137 private final Sources mSources;
Evan Millaradb0f8c2009-09-28 17:20:50 -0700138
Dmitri Plotnikov3f5686e2010-03-05 10:30:03 -0800139 public PhonesAdapter(Context context, List<PhoneItem> objects, boolean sendSms) {
140 super(context, R.layout.phone_disambig_item,
141 android.R.id.text2, objects);
142 this.sendSms = sendSms;
143 mSources = Sources.getInstance(context);
144 }
145
146 @Override
147 public View getView(int position, View convertView, ViewGroup parent) {
148 View view = super.getView(position, convertView, parent);
149
150 PhoneItem item = getItem(position);
151 ContactsSource source = mSources.getInflatedSource(item.accountType,
152 ContactsSource.LEVEL_SUMMARY);
153
154 // Obtain a string representation of the phone type specific to the
155 // ContactSource associated with that phone number
156 TextView typeView = (TextView)view.findViewById(android.R.id.text1);
157 DataKind kind = source.getKindForMimetype(Phone.CONTENT_ITEM_TYPE);
Dmitri Plotnikov4a261c82010-04-05 18:53:05 -0700158 if (kind != null) {
159 ContentValues values = new ContentValues();
160 values.put(Phone.TYPE, item.type);
161 values.put(Phone.LABEL, item.label);
162 StringInflater header = sendSms ? kind.actionAltHeader : kind.actionHeader;
163 typeView.setText(header.inflateUsing(getContext(), values));
164 } else {
165 typeView.setText(R.string.call_other);
166 }
Dmitri Plotnikov3f5686e2010-03-05 10:30:03 -0800167 return view;
Evan Millaradb0f8c2009-09-28 17:20:50 -0700168 }
169 }
170
171 private class PhoneItem implements Collapsible<PhoneItem> {
172
Daisuke Miyakawa46befc32009-10-28 10:13:57 +0900173 final long id;
Dmitri Plotnikov3f5686e2010-03-05 10:30:03 -0800174 final String phoneNumber;
175 final String accountType;
176 final long type;
177 final String label;
Evan Millaradb0f8c2009-09-28 17:20:50 -0700178
Dmitri Plotnikov3f5686e2010-03-05 10:30:03 -0800179 public PhoneItem(long id, String phoneNumber, String accountType, int type, String label) {
180 this.id = id;
181 this.phoneNumber = (phoneNumber != null ? phoneNumber : "");
182 this.accountType = accountType;
183 this.type = type;
184 this.label = label;
Evan Millaradb0f8c2009-09-28 17:20:50 -0700185 }
186
187 public boolean collapseWith(PhoneItem phoneItem) {
188 if (!shouldCollapseWith(phoneItem)) {
189 return false;
190 }
191 // Just keep the number and id we already have.
192 return true;
193 }
194
195 public boolean shouldCollapseWith(PhoneItem phoneItem) {
196 if (PhoneNumberUtils.compare(PhoneDisambigDialog.this.mContext,
197 phoneNumber, phoneItem.phoneNumber)) {
198 return true;
199 }
200 return false;
201 }
202
Daisuke Miyakawa46befc32009-10-28 10:13:57 +0900203 @Override
Evan Millaradb0f8c2009-09-28 17:20:50 -0700204 public String toString() {
205 return phoneNumber;
206 }
207 }
208
209 private ArrayList<PhoneItem> makePhoneItemsList(Cursor phonesCursor) {
210 ArrayList<PhoneItem> phoneList = new ArrayList<PhoneItem>();
211
212 phonesCursor.moveToPosition(-1);
213 while (phonesCursor.moveToNext()) {
214 long id = phonesCursor.getLong(phonesCursor.getColumnIndex(Data._ID));
215 String phone = phonesCursor.getString(phonesCursor.getColumnIndex(Phone.NUMBER));
Dmitri Plotnikov3f5686e2010-03-05 10:30:03 -0800216 String accountType =
217 phonesCursor.getString(phonesCursor.getColumnIndex(RawContacts.ACCOUNT_TYPE));
218 int type = phonesCursor.getInt(phonesCursor.getColumnIndex(Phone.TYPE));
219 String label = phonesCursor.getString(phonesCursor.getColumnIndex(Phone.LABEL));
220
221 phoneList.add(new PhoneItem(id, phone, accountType, type, label));
Evan Millaradb0f8c2009-09-28 17:20:50 -0700222 }
223
224 return phoneList;
225 }
Evan Millar14fecb62009-09-09 09:23:12 -0700226}