blob: b727c774b571f00c851d77cb518be641fc84fea3 [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
19import java.util.ArrayList;
Evan Millaradb0f8c2009-09-28 17:20:50 -070020import java.util.List;
21
22import com.android.contacts.Collapser.Collapsible;
Evan Millar14fecb62009-09-09 09:23:12 -070023
24import android.app.AlertDialog;
25import android.content.ContentUris;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.DialogInterface;
29import android.database.Cursor;
30import android.provider.ContactsContract.Data;
31import android.provider.ContactsContract.CommonDataKinds.Phone;
Evan Millaradb0f8c2009-09-28 17:20:50 -070032import android.telephony.PhoneNumberUtils;
Evan Millar14fecb62009-09-09 09:23:12 -070033import android.view.LayoutInflater;
34import android.view.View;
35import android.widget.ArrayAdapter;
36import android.widget.CheckBox;
37import android.widget.CompoundButton;
Evan Millaradb0f8c2009-09-28 17:20:50 -070038import android.widget.ListAdapter;
Evan Millar14fecb62009-09-09 09:23:12 -070039
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 */
44public 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 Millaradb0f8c2009-09-28 17:20:50 -070052 private ListAdapter mPhonesAdapter;
53 private ArrayList<PhoneItem> mPhoneItemList;
Evan Millar14fecb62009-09-09 09:23:12 -070054
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 Millaradb0f8c2009-09-28 17:20:50 -070064 mPhoneItemList = makePhoneItemsList(phonesCursor);
65 Collapser.collapseList(mPhoneItemList);
66
67 mPhonesAdapter = new PhonesAdapter(mContext, mPhoneItemList);
68
Evan Millar14fecb62009-09-09 09:23:12 -070069 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 Millaradb0f8c2009-09-28 17:20:50 -070078 setAdapter(mPhonesAdapter, this).
79 setTitle(sendSms ?
80 R.string.sms_disambig_title : R.string.call_disambig_title).
81 setView(setPrimaryView);
Evan Millar14fecb62009-09-09 09:23:12 -070082
83 mDialog = dialogBuilder.create();
84 }
85
86 /**
87 * Show the dialog.
88 */
89 public void show() {
Evan Millaradb0f8c2009-09-28 17:20:50 -070090 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 Millar14fecb62009-09-09 09:23:12 -070095 mDialog.show();
96 }
97
98 public void onClick(DialogInterface dialog, int which) {
Evan Millaradb0f8c2009-09-28 17:20:50 -070099 if (mPhoneItemList.size() > which && which >= 0) {
100 PhoneItem phoneItem = mPhoneItemList.get(which);
101 long id = phoneItem.id;
102 String phone = phoneItem.phoneNumber;
103
Evan Millar14fecb62009-09-09 09:23:12 -0700104 if (mMakePrimary) {
105 ContentValues values = new ContentValues(1);
106 values.put(Data.IS_SUPER_PRIMARY, 1);
Evan Millaradb0f8c2009-09-28 17:20:50 -0700107 mContext.getContentResolver().update(ContentUris.
108 withAppendedId(Data.CONTENT_URI, id), values, null, null);
Evan Millar14fecb62009-09-09 09:23:12 -0700109 }
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 Millaradb0f8c2009-09-28 17:20:50 -0700128
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 Millar14fecb62009-09-09 09:23:12 -0700180}