Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.contacts; |
| 18 | |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 19 | import com.android.contacts.Collapser.Collapsible; |
Dmitri Plotnikov | 3f5686e | 2010-03-05 10:30:03 -0800 | [diff] [blame] | 20 | import com.android.contacts.model.ContactsSource; |
| 21 | import com.android.contacts.model.Sources; |
| 22 | import com.android.contacts.model.ContactsSource.DataKind; |
| 23 | import com.android.contacts.model.ContactsSource.StringInflater; |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 24 | |
| 25 | import android.app.AlertDialog; |
| 26 | import android.content.ContentUris; |
| 27 | import android.content.ContentValues; |
| 28 | import android.content.Context; |
| 29 | import android.content.DialogInterface; |
| 30 | import android.database.Cursor; |
| 31 | import android.provider.ContactsContract.Data; |
Dmitri Plotnikov | 3f5686e | 2010-03-05 10:30:03 -0800 | [diff] [blame] | 32 | import android.provider.ContactsContract.RawContacts; |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 33 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 34 | import android.telephony.PhoneNumberUtils; |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 35 | import android.view.LayoutInflater; |
| 36 | import android.view.View; |
Dmitri Plotnikov | 3f5686e | 2010-03-05 10:30:03 -0800 | [diff] [blame] | 37 | import android.view.ViewGroup; |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 38 | import android.widget.ArrayAdapter; |
| 39 | import android.widget.CheckBox; |
| 40 | import android.widget.CompoundButton; |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 41 | import android.widget.ListAdapter; |
Dmitri Plotnikov | 3f5686e | 2010-03-05 10:30:03 -0800 | [diff] [blame] | 42 | import android.widget.TextView; |
| 43 | |
| 44 | import java.util.ArrayList; |
| 45 | import java.util.List; |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 46 | |
| 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 | */ |
| 51 | public class PhoneDisambigDialog implements DialogInterface.OnClickListener, |
Dmitri Plotnikov | 7c5286e | 2010-05-04 16:49:36 -0700 | [diff] [blame] | 52 | CompoundButton.OnCheckedChangeListener{ |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 53 | |
| 54 | private boolean mMakePrimary = false; |
| 55 | private Context mContext; |
| 56 | private AlertDialog mDialog; |
| 57 | private boolean mSendSms; |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 58 | private ListAdapter mPhonesAdapter; |
| 59 | private ArrayList<PhoneItem> mPhoneItemList; |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 60 | |
| 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 Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 68 | mPhoneItemList = makePhoneItemsList(phonesCursor); |
Dmitri Plotnikov | 7c5286e | 2010-05-04 16:49:36 -0700 | [diff] [blame] | 69 | phonesCursor.close(); |
| 70 | |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 71 | Collapser.collapseList(mPhoneItemList); |
| 72 | |
Dmitri Plotnikov | 3f5686e | 2010-03-05 10:30:03 -0800 | [diff] [blame] | 73 | mPhonesAdapter = new PhonesAdapter(mContext, mPhoneItemList, mSendSms); |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 74 | |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 75 | 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 Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 84 | setAdapter(mPhonesAdapter, this). |
| 85 | setTitle(sendSms ? |
| 86 | R.string.sms_disambig_title : R.string.call_disambig_title). |
| 87 | setView(setPrimaryView); |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 88 | |
| 89 | mDialog = dialogBuilder.create(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Show the dialog. |
| 94 | */ |
| 95 | public void show() { |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 96 | if (mPhoneItemList.size() == 1) { |
| 97 | // If there is only one after collapse, just select it, and close; |
| 98 | onClick(mDialog, 0); |
| 99 | return; |
| 100 | } |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 101 | mDialog.show(); |
| 102 | } |
| 103 | |
| 104 | public void onClick(DialogInterface dialog, int which) { |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 105 | if (mPhoneItemList.size() > which && which >= 0) { |
| 106 | PhoneItem phoneItem = mPhoneItemList.get(which); |
| 107 | long id = phoneItem.id; |
| 108 | String phone = phoneItem.phoneNumber; |
| 109 | |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 110 | if (mMakePrimary) { |
| 111 | ContentValues values = new ContentValues(1); |
| 112 | values.put(Data.IS_SUPER_PRIMARY, 1); |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 113 | mContext.getContentResolver().update(ContentUris. |
| 114 | withAppendedId(Data.CONTENT_URI, id), values, null, null); |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | if (mSendSms) { |
| 118 | ContactsUtils.initiateSms(mContext, phone); |
| 119 | } else { |
| 120 | ContactsUtils.initiateCall(mContext, phone); |
| 121 | } |
| 122 | } else { |
| 123 | dialog.dismiss(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 128 | mMakePrimary = isChecked; |
| 129 | } |
| 130 | |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 131 | private static class PhonesAdapter extends ArrayAdapter<PhoneItem> { |
Dmitri Plotnikov | 3f5686e | 2010-03-05 10:30:03 -0800 | [diff] [blame] | 132 | private final boolean sendSms; |
| 133 | private final Sources mSources; |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 134 | |
Dmitri Plotnikov | 3f5686e | 2010-03-05 10:30:03 -0800 | [diff] [blame] | 135 | public PhonesAdapter(Context context, List<PhoneItem> objects, boolean sendSms) { |
| 136 | super(context, R.layout.phone_disambig_item, |
| 137 | android.R.id.text2, objects); |
| 138 | this.sendSms = sendSms; |
| 139 | mSources = Sources.getInstance(context); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public View getView(int position, View convertView, ViewGroup parent) { |
| 144 | View view = super.getView(position, convertView, parent); |
| 145 | |
| 146 | PhoneItem item = getItem(position); |
| 147 | ContactsSource source = mSources.getInflatedSource(item.accountType, |
| 148 | ContactsSource.LEVEL_SUMMARY); |
| 149 | |
| 150 | // Obtain a string representation of the phone type specific to the |
| 151 | // ContactSource associated with that phone number |
| 152 | TextView typeView = (TextView)view.findViewById(android.R.id.text1); |
| 153 | DataKind kind = source.getKindForMimetype(Phone.CONTENT_ITEM_TYPE); |
Dmitri Plotnikov | 4a261c8 | 2010-04-05 18:53:05 -0700 | [diff] [blame] | 154 | if (kind != null) { |
| 155 | ContentValues values = new ContentValues(); |
| 156 | values.put(Phone.TYPE, item.type); |
| 157 | values.put(Phone.LABEL, item.label); |
| 158 | StringInflater header = sendSms ? kind.actionAltHeader : kind.actionHeader; |
| 159 | typeView.setText(header.inflateUsing(getContext(), values)); |
| 160 | } else { |
| 161 | typeView.setText(R.string.call_other); |
| 162 | } |
Dmitri Plotnikov | 3f5686e | 2010-03-05 10:30:03 -0800 | [diff] [blame] | 163 | return view; |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | private class PhoneItem implements Collapsible<PhoneItem> { |
| 168 | |
Daisuke Miyakawa | 46befc3 | 2009-10-28 10:13:57 +0900 | [diff] [blame] | 169 | final long id; |
Dmitri Plotnikov | 3f5686e | 2010-03-05 10:30:03 -0800 | [diff] [blame] | 170 | final String phoneNumber; |
| 171 | final String accountType; |
| 172 | final long type; |
| 173 | final String label; |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 174 | |
Dmitri Plotnikov | 3f5686e | 2010-03-05 10:30:03 -0800 | [diff] [blame] | 175 | public PhoneItem(long id, String phoneNumber, String accountType, int type, String label) { |
| 176 | this.id = id; |
| 177 | this.phoneNumber = (phoneNumber != null ? phoneNumber : ""); |
| 178 | this.accountType = accountType; |
| 179 | this.type = type; |
| 180 | this.label = label; |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | public boolean collapseWith(PhoneItem phoneItem) { |
| 184 | if (!shouldCollapseWith(phoneItem)) { |
| 185 | return false; |
| 186 | } |
| 187 | // Just keep the number and id we already have. |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | public boolean shouldCollapseWith(PhoneItem phoneItem) { |
| 192 | if (PhoneNumberUtils.compare(PhoneDisambigDialog.this.mContext, |
| 193 | phoneNumber, phoneItem.phoneNumber)) { |
| 194 | return true; |
| 195 | } |
| 196 | return false; |
| 197 | } |
| 198 | |
Daisuke Miyakawa | 46befc3 | 2009-10-28 10:13:57 +0900 | [diff] [blame] | 199 | @Override |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 200 | public String toString() { |
| 201 | return phoneNumber; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | private ArrayList<PhoneItem> makePhoneItemsList(Cursor phonesCursor) { |
| 206 | ArrayList<PhoneItem> phoneList = new ArrayList<PhoneItem>(); |
| 207 | |
| 208 | phonesCursor.moveToPosition(-1); |
| 209 | while (phonesCursor.moveToNext()) { |
| 210 | long id = phonesCursor.getLong(phonesCursor.getColumnIndex(Data._ID)); |
| 211 | String phone = phonesCursor.getString(phonesCursor.getColumnIndex(Phone.NUMBER)); |
Dmitri Plotnikov | 3f5686e | 2010-03-05 10:30:03 -0800 | [diff] [blame] | 212 | String accountType = |
| 213 | phonesCursor.getString(phonesCursor.getColumnIndex(RawContacts.ACCOUNT_TYPE)); |
| 214 | int type = phonesCursor.getInt(phonesCursor.getColumnIndex(Phone.TYPE)); |
| 215 | String label = phonesCursor.getString(phonesCursor.getColumnIndex(Phone.LABEL)); |
| 216 | |
| 217 | phoneList.add(new PhoneItem(id, phone, accountType, type, label)); |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | return phoneList; |
| 221 | } |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 222 | } |