blob: 58d3721092de1aed31013e77ebe3f30e0b4aacea [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;
20import java.util.HashMap;
21import java.util.HashSet;
22
23import android.app.AlertDialog;
24import android.content.ContentUris;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.DialogInterface;
28import android.database.Cursor;
29import android.provider.ContactsContract.Data;
30import android.provider.ContactsContract.CommonDataKinds.Phone;
31import android.view.LayoutInflater;
32import android.view.View;
33import android.widget.ArrayAdapter;
34import android.widget.CheckBox;
35import android.widget.CompoundButton;
36import android.widget.SimpleCursorAdapter;
37
38/**
39 * Class used for displaying a dialog with a list of phone numbers of which
40 * one will be chosen to make a call or initiate an sms message.
41 */
42public class PhoneDisambigDialog implements DialogInterface.OnClickListener,
43 DialogInterface.OnDismissListener, CompoundButton.OnCheckedChangeListener{
44
45 private boolean mMakePrimary = false;
46 private Context mContext;
47 private AlertDialog mDialog;
48 private boolean mSendSms;
49 private Cursor mPhonesCursor;
50
51 public PhoneDisambigDialog(Context context, Cursor phonesCursor) {
52 this(context, phonesCursor, false /*make call*/);
53 }
54
55 public PhoneDisambigDialog(Context context, Cursor phonesCursor, boolean sendSms) {
56 mContext = context;
57 mSendSms = sendSms;
58 mPhonesCursor = phonesCursor;
59
60 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
61 Context.LAYOUT_INFLATER_SERVICE);
62 View setPrimaryView = inflater.
63 inflate(R.layout.set_primary_checkbox, null);
64 ((CheckBox) setPrimaryView.findViewById(R.id.setPrimary)).
65 setOnCheckedChangeListener(this);
66
67 // Need to show disambig dialogue.
68 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext).
69 setCursor(mPhonesCursor, this, Phone.NUMBER).
70 setTitle(sendSms ? R.string.sms_disambig_title : R.string.call_disambig_title).
71 setView(setPrimaryView);
72
73 mDialog = dialogBuilder.create();
74 }
75
76 /**
77 * Show the dialog.
78 */
79 public void show() {
80 mDialog.show();
81 }
82
83 public void onClick(DialogInterface dialog, int which) {
84 if (mPhonesCursor.moveToPosition(which)) {
85 long id = mPhonesCursor.getLong(mPhonesCursor.getColumnIndex(Data._ID));
86 String phone = mPhonesCursor.getString(mPhonesCursor.getColumnIndex(Phone.NUMBER));
87 if (mMakePrimary) {
88 ContentValues values = new ContentValues(1);
89 values.put(Data.IS_SUPER_PRIMARY, 1);
90 mContext.getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, id),
91 values, null, null);
92 }
93
94 if (mSendSms) {
95 ContactsUtils.initiateSms(mContext, phone);
96 } else {
97 ContactsUtils.initiateCall(mContext, phone);
98 }
99 } else {
100 dialog.dismiss();
101 }
102 }
103
104 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
105 mMakePrimary = isChecked;
106 }
107
108 public void onDismiss(DialogInterface dialog) {
109 mPhonesCursor.close();
110 }
111}