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 | |
| 19 | import java.util.ArrayList; |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame^] | 20 | import java.util.List; |
| 21 | |
| 22 | import com.android.contacts.Collapser.Collapsible; |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 23 | |
| 24 | import android.app.AlertDialog; |
| 25 | import android.content.ContentUris; |
| 26 | import android.content.ContentValues; |
| 27 | import android.content.Context; |
| 28 | import android.content.DialogInterface; |
| 29 | import android.database.Cursor; |
| 30 | import android.provider.ContactsContract.Data; |
| 31 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame^] | 32 | import android.telephony.PhoneNumberUtils; |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 33 | import android.view.LayoutInflater; |
| 34 | import android.view.View; |
| 35 | import android.widget.ArrayAdapter; |
| 36 | import android.widget.CheckBox; |
| 37 | import android.widget.CompoundButton; |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame^] | 38 | import android.widget.ListAdapter; |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Class used for displaying a dialog with a list of phone numbers of which |
| 42 | * one will be chosen to make a call or initiate an sms message. |
| 43 | */ |
| 44 | public class PhoneDisambigDialog implements DialogInterface.OnClickListener, |
| 45 | DialogInterface.OnDismissListener, CompoundButton.OnCheckedChangeListener{ |
| 46 | |
| 47 | private boolean mMakePrimary = false; |
| 48 | private Context mContext; |
| 49 | private AlertDialog mDialog; |
| 50 | private boolean mSendSms; |
| 51 | private Cursor mPhonesCursor; |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame^] | 52 | private ListAdapter mPhonesAdapter; |
| 53 | private ArrayList<PhoneItem> mPhoneItemList; |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 54 | |
| 55 | public PhoneDisambigDialog(Context context, Cursor phonesCursor) { |
| 56 | this(context, phonesCursor, false /*make call*/); |
| 57 | } |
| 58 | |
| 59 | public PhoneDisambigDialog(Context context, Cursor phonesCursor, boolean sendSms) { |
| 60 | mContext = context; |
| 61 | mSendSms = sendSms; |
| 62 | mPhonesCursor = phonesCursor; |
| 63 | |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame^] | 64 | mPhoneItemList = makePhoneItemsList(phonesCursor); |
| 65 | Collapser.collapseList(mPhoneItemList); |
| 66 | |
| 67 | mPhonesAdapter = new PhonesAdapter(mContext, mPhoneItemList); |
| 68 | |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 69 | LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( |
| 70 | Context.LAYOUT_INFLATER_SERVICE); |
| 71 | View setPrimaryView = inflater. |
| 72 | inflate(R.layout.set_primary_checkbox, null); |
| 73 | ((CheckBox) setPrimaryView.findViewById(R.id.setPrimary)). |
| 74 | setOnCheckedChangeListener(this); |
| 75 | |
| 76 | // Need to show disambig dialogue. |
| 77 | AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext). |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame^] | 78 | setAdapter(mPhonesAdapter, this). |
| 79 | setTitle(sendSms ? |
| 80 | R.string.sms_disambig_title : R.string.call_disambig_title). |
| 81 | setView(setPrimaryView); |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 82 | |
| 83 | mDialog = dialogBuilder.create(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Show the dialog. |
| 88 | */ |
| 89 | public void show() { |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame^] | 90 | if (mPhoneItemList.size() == 1) { |
| 91 | // If there is only one after collapse, just select it, and close; |
| 92 | onClick(mDialog, 0); |
| 93 | return; |
| 94 | } |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 95 | mDialog.show(); |
| 96 | } |
| 97 | |
| 98 | public void onClick(DialogInterface dialog, int which) { |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame^] | 99 | if (mPhoneItemList.size() > which && which >= 0) { |
| 100 | PhoneItem phoneItem = mPhoneItemList.get(which); |
| 101 | long id = phoneItem.id; |
| 102 | String phone = phoneItem.phoneNumber; |
| 103 | |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 104 | if (mMakePrimary) { |
| 105 | ContentValues values = new ContentValues(1); |
| 106 | values.put(Data.IS_SUPER_PRIMARY, 1); |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame^] | 107 | mContext.getContentResolver().update(ContentUris. |
| 108 | withAppendedId(Data.CONTENT_URI, id), values, null, null); |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | if (mSendSms) { |
| 112 | ContactsUtils.initiateSms(mContext, phone); |
| 113 | } else { |
| 114 | ContactsUtils.initiateCall(mContext, phone); |
| 115 | } |
| 116 | } else { |
| 117 | dialog.dismiss(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 122 | mMakePrimary = isChecked; |
| 123 | } |
| 124 | |
| 125 | public void onDismiss(DialogInterface dialog) { |
| 126 | mPhonesCursor.close(); |
| 127 | } |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame^] | 128 | |
| 129 | private static class PhonesAdapter extends ArrayAdapter<PhoneItem> { |
| 130 | |
| 131 | public PhonesAdapter(Context context, List<PhoneItem> objects) { |
| 132 | super(context, android.R.layout.simple_dropdown_item_1line, |
| 133 | android.R.id.text1, objects); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | private class PhoneItem implements Collapsible<PhoneItem> { |
| 138 | |
| 139 | String phoneNumber; |
| 140 | long id; |
| 141 | |
| 142 | public PhoneItem(String newPhoneNumber, long newId) { |
| 143 | phoneNumber = newPhoneNumber; |
| 144 | id = newId; |
| 145 | } |
| 146 | |
| 147 | public boolean collapseWith(PhoneItem phoneItem) { |
| 148 | if (!shouldCollapseWith(phoneItem)) { |
| 149 | return false; |
| 150 | } |
| 151 | // Just keep the number and id we already have. |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | public boolean shouldCollapseWith(PhoneItem phoneItem) { |
| 156 | if (PhoneNumberUtils.compare(PhoneDisambigDialog.this.mContext, |
| 157 | phoneNumber, phoneItem.phoneNumber)) { |
| 158 | return true; |
| 159 | } |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | public String toString() { |
| 164 | return phoneNumber; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | private ArrayList<PhoneItem> makePhoneItemsList(Cursor phonesCursor) { |
| 169 | ArrayList<PhoneItem> phoneList = new ArrayList<PhoneItem>(); |
| 170 | |
| 171 | phonesCursor.moveToPosition(-1); |
| 172 | while (phonesCursor.moveToNext()) { |
| 173 | long id = phonesCursor.getLong(phonesCursor.getColumnIndex(Data._ID)); |
| 174 | String phone = phonesCursor.getString(phonesCursor.getColumnIndex(Phone.NUMBER)); |
| 175 | phoneList.add(new PhoneItem(phone, id)); |
| 176 | } |
| 177 | |
| 178 | return phoneList; |
| 179 | } |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 180 | } |