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; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.HashSet; |
| 22 | |
| 23 | import android.app.AlertDialog; |
| 24 | import android.content.ContentUris; |
| 25 | import android.content.ContentValues; |
| 26 | import android.content.Context; |
| 27 | import android.content.DialogInterface; |
| 28 | import android.database.Cursor; |
| 29 | import android.provider.ContactsContract.Data; |
| 30 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 31 | import android.view.LayoutInflater; |
| 32 | import android.view.View; |
| 33 | import android.widget.ArrayAdapter; |
| 34 | import android.widget.CheckBox; |
| 35 | import android.widget.CompoundButton; |
| 36 | import 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 | */ |
| 42 | public 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 | } |