Making call/sms interaction preserve dialog state
And also cleaning up the implementation
Change-Id: I6d9ca845477282d4ef9db7417e99cffc9e608c86
diff --git a/res/values/ids.xml b/res/values/ids.xml
index 1846824..d71a8d2 100644
--- a/res/values/ids.xml
+++ b/res/values/ids.xml
@@ -60,4 +60,10 @@
<item type="id" name="dialog_exporting_vcard"/>
<item type="id" name="dialog_fail_to_export_with_reason"/>
+ <!-- For PhoneNumberCallInteraction -->
+ <item type="id" name="dialog_phone_number_call_disambiguation"/>
+
+ <!-- For PhoneNumberMessageSendInteraction -->
+ <item type="id" name="dialog_phone_number_message_disambiguation"/>
+
</resources>
diff --git a/src/com/android/contacts/CallContactActivity.java b/src/com/android/contacts/CallContactActivity.java
index 181e9b7..76f0892 100644
--- a/src/com/android/contacts/CallContactActivity.java
+++ b/src/com/android/contacts/CallContactActivity.java
@@ -16,12 +16,13 @@
package com.android.contacts;
-import com.android.contacts.list.CallOrSmsInitiator;
+import com.android.contacts.interactions.PhoneNumberInteraction;
import android.app.Activity;
+import android.app.Dialog;
import android.content.DialogInterface;
-import android.content.Intent;
import android.content.DialogInterface.OnDismissListener;
+import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
@@ -32,26 +33,44 @@
*/
public class CallContactActivity extends Activity implements OnDismissListener {
+ private PhoneNumberInteraction mPhoneNumberInteraction;
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+ mPhoneNumberInteraction = new PhoneNumberInteraction(this, false, this);
- Uri data = getIntent().getData();
- if (data == null) {
+ Uri contactUri = getIntent().getData();
+ if (contactUri == null) {
finish();
}
- if (Contacts.CONTENT_ITEM_TYPE.equals(getContentResolver().getType(data))) {
- CallOrSmsInitiator initiator = new CallOrSmsInitiator(this);
- initiator.setOnDismissListener(this);
- initiator.initiateCall(data);
+ // If we are being invoked with a saved state, rely on Activity to restore it
+ if (savedInstanceState != null) {
+ return;
+ }
+
+ if (Contacts.CONTENT_ITEM_TYPE.equals(getContentResolver().getType(contactUri))) {
+ mPhoneNumberInteraction.startInteraction(contactUri);
} else {
- startActivity(new Intent(Intent.ACTION_CALL_PRIVILEGED, data));
+ startActivity(new Intent(Intent.ACTION_CALL_PRIVILEGED, contactUri));
finish();
}
}
public void onDismiss(DialogInterface dialog) {
- finish();
+ if (!isChangingConfigurations()) {
+ finish();
+ }
+ }
+
+ @Override
+ protected Dialog onCreateDialog(int id, Bundle args) {
+ return mPhoneNumberInteraction.onCreateDialog(id, args);
+ }
+
+ @Override
+ protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
+ mPhoneNumberInteraction.onPrepareDialog(id, dialog, args);
}
}
diff --git a/src/com/android/contacts/ContactsListActivity.java b/src/com/android/contacts/ContactsListActivity.java
index 4958be6..2c75051 100644
--- a/src/com/android/contacts/ContactsListActivity.java
+++ b/src/com/android/contacts/ContactsListActivity.java
@@ -17,7 +17,7 @@
package com.android.contacts;
import com.android.contacts.interactions.ContactDeletionInteraction;
-import com.android.contacts.list.CallOrSmsInitiator;
+import com.android.contacts.interactions.PhoneNumberInteraction;
import com.android.contacts.list.ContactBrowseListContextMenuAdapter;
import com.android.contacts.list.ContactEntryListFragment;
import com.android.contacts.list.ContactPickerFragment;
@@ -90,7 +90,8 @@
private ContactsIntentResolver mIntentResolver;
protected ContactEntryListFragment<?> mListFragment;
- protected CallOrSmsInitiator mCallOrSmsInitiator;
+ protected PhoneNumberInteraction mPhoneNumberCallInteraction;
+ protected PhoneNumberInteraction mSendTextMessageInteraction;
private ContactDeletionInteraction mContactDeletionInteraction;
private int mActionCode;
@@ -338,11 +339,11 @@
}
public void onCallContactAction(Uri contactUri) {
- getCallOrSmsInitiator().initiateCall(contactUri);
+ getPhoneNumberCallInteraction().startInteraction(contactUri);
}
public void onSmsContactAction(Uri contactUri) {
- getCallOrSmsInitiator().initiateSms(contactUri);
+ getSendTextMessageInteraction().startInteraction(contactUri);
}
public void onDeleteContactAction(Uri contactUri) {
@@ -489,6 +490,16 @@
return dialog;
}
+ dialog = getPhoneNumberCallInteraction().onCreateDialog(id, bundle);
+ if (dialog != null) {
+ return dialog;
+ }
+
+ dialog = getSendTextMessageInteraction().onCreateDialog(id, bundle);
+ if (dialog != null) {
+ return dialog;
+ }
+
switch (id) {
case R.string.import_from_sim:
case R.string.import_from_sdcard: {
@@ -511,6 +522,14 @@
return;
}
+ if (getPhoneNumberCallInteraction().onPrepareDialog(id, dialog, bundle)) {
+ return;
+ }
+
+ if (getSendTextMessageInteraction().onPrepareDialog(id, dialog, bundle)) {
+ return;
+ }
+
super.onPrepareDialog(id, dialog, bundle);
}
/**
@@ -737,11 +756,18 @@
return false;
}
- private CallOrSmsInitiator getCallOrSmsInitiator() {
- if (mCallOrSmsInitiator == null) {
- mCallOrSmsInitiator = new CallOrSmsInitiator(this);
+ private PhoneNumberInteraction getPhoneNumberCallInteraction() {
+ if (mPhoneNumberCallInteraction == null) {
+ mPhoneNumberCallInteraction = new PhoneNumberInteraction(this, false, null);
}
- return mCallOrSmsInitiator;
+ return mPhoneNumberCallInteraction;
+ }
+
+ private PhoneNumberInteraction getSendTextMessageInteraction() {
+ if (mSendTextMessageInteraction == null) {
+ mSendTextMessageInteraction = new PhoneNumberInteraction(this, true, null);
+ }
+ return mSendTextMessageInteraction;
}
private ContactDeletionInteraction getContactDeletionInteraction() {
diff --git a/src/com/android/contacts/PhoneDisambigDialog.java b/src/com/android/contacts/PhoneDisambigDialog.java
deleted file mode 100644
index 3b32b57..0000000
--- a/src/com/android/contacts/PhoneDisambigDialog.java
+++ /dev/null
@@ -1,226 +0,0 @@
-/*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.contacts;
-
-import com.android.contacts.Collapser.Collapsible;
-import com.android.contacts.model.ContactsSource;
-import com.android.contacts.model.Sources;
-import com.android.contacts.model.ContactsSource.DataKind;
-import com.android.contacts.model.ContactsSource.StringInflater;
-
-import android.app.AlertDialog;
-import android.content.ContentUris;
-import android.content.ContentValues;
-import android.content.Context;
-import android.content.DialogInterface;
-import android.database.Cursor;
-import android.provider.ContactsContract.Data;
-import android.provider.ContactsContract.RawContacts;
-import android.provider.ContactsContract.CommonDataKinds.Phone;
-import android.telephony.PhoneNumberUtils;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.ArrayAdapter;
-import android.widget.CheckBox;
-import android.widget.CompoundButton;
-import android.widget.ListAdapter;
-import android.widget.TextView;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * Class used for displaying a dialog with a list of phone numbers of which
- * one will be chosen to make a call or initiate an sms message.
- */
-public class PhoneDisambigDialog implements DialogInterface.OnClickListener,
- CompoundButton.OnCheckedChangeListener{
-
- private boolean mMakePrimary = false;
- private Context mContext;
- private AlertDialog mDialog;
- private boolean mSendSms;
- private ListAdapter mPhonesAdapter;
- private ArrayList<PhoneItem> mPhoneItemList;
-
- public PhoneDisambigDialog(Context context, Cursor phonesCursor) {
- this(context, phonesCursor, false /*make call*/);
- }
-
- public PhoneDisambigDialog(Context context, Cursor phonesCursor, boolean sendSms) {
- mContext = context;
- mSendSms = sendSms;
- mPhoneItemList = makePhoneItemsList(phonesCursor);
- phonesCursor.close();
-
- Collapser.collapseList(mPhoneItemList);
-
- mPhonesAdapter = new PhonesAdapter(mContext, mPhoneItemList, mSendSms);
-
- LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
- Context.LAYOUT_INFLATER_SERVICE);
- View setPrimaryView = inflater.
- inflate(R.layout.set_primary_checkbox, null);
- ((CheckBox) setPrimaryView.findViewById(R.id.setPrimary)).
- setOnCheckedChangeListener(this);
-
- // Need to show disambig dialogue.
- AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext).
- setAdapter(mPhonesAdapter, this).
- setTitle(sendSms ?
- R.string.sms_disambig_title : R.string.call_disambig_title).
- setView(setPrimaryView);
-
- mDialog = dialogBuilder.create();
- }
-
- public void setOnDismissListener(DialogInterface.OnDismissListener dismissListener) {
- mDialog.setOnDismissListener(dismissListener);
- }
-
- /**
- * Show the dialog.
- */
- public void show() {
- if (mPhoneItemList.size() == 1) {
- // If there is only one after collapse, just select it, and close;
- onClick(mDialog, 0);
- return;
- }
- mDialog.show();
- }
-
- public void onClick(DialogInterface dialog, int which) {
- if (mPhoneItemList.size() > which && which >= 0) {
- PhoneItem phoneItem = mPhoneItemList.get(which);
- long id = phoneItem.id;
- String phone = phoneItem.phoneNumber;
-
- if (mMakePrimary) {
- ContentValues values = new ContentValues(1);
- values.put(Data.IS_SUPER_PRIMARY, 1);
- mContext.getContentResolver().update(ContentUris.
- withAppendedId(Data.CONTENT_URI, id), values, null, null);
- }
-
- if (mSendSms) {
- ContactsUtils.initiateSms(mContext, phone);
- } else {
- ContactsUtils.initiateCall(mContext, phone);
- }
- } else {
- dialog.dismiss();
- }
- }
-
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- mMakePrimary = isChecked;
- }
-
- private static class PhonesAdapter extends ArrayAdapter<PhoneItem> {
- private final boolean sendSms;
- private final Sources mSources;
-
- public PhonesAdapter(Context context, List<PhoneItem> objects, boolean sendSms) {
- super(context, R.layout.phone_disambig_item,
- android.R.id.text2, objects);
- this.sendSms = sendSms;
- mSources = Sources.getInstance(context);
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View view = super.getView(position, convertView, parent);
-
- PhoneItem item = getItem(position);
- ContactsSource source = mSources.getInflatedSource(item.accountType,
- ContactsSource.LEVEL_SUMMARY);
-
- // Obtain a string representation of the phone type specific to the
- // ContactSource associated with that phone number
- TextView typeView = (TextView)view.findViewById(android.R.id.text1);
- DataKind kind = source.getKindForMimetype(Phone.CONTENT_ITEM_TYPE);
- if (kind != null) {
- ContentValues values = new ContentValues();
- values.put(Phone.TYPE, item.type);
- values.put(Phone.LABEL, item.label);
- StringInflater header = sendSms ? kind.actionAltHeader : kind.actionHeader;
- typeView.setText(header.inflateUsing(getContext(), values));
- } else {
- typeView.setText(R.string.call_other);
- }
- return view;
- }
- }
-
- private class PhoneItem implements Collapsible<PhoneItem> {
-
- final long id;
- final String phoneNumber;
- final String accountType;
- final long type;
- final String label;
-
- public PhoneItem(long id, String phoneNumber, String accountType, int type, String label) {
- this.id = id;
- this.phoneNumber = (phoneNumber != null ? phoneNumber : "");
- this.accountType = accountType;
- this.type = type;
- this.label = label;
- }
-
- public boolean collapseWith(PhoneItem phoneItem) {
- if (!shouldCollapseWith(phoneItem)) {
- return false;
- }
- // Just keep the number and id we already have.
- return true;
- }
-
- public boolean shouldCollapseWith(PhoneItem phoneItem) {
- if (PhoneNumberUtils.compare(PhoneDisambigDialog.this.mContext,
- phoneNumber, phoneItem.phoneNumber)) {
- return true;
- }
- return false;
- }
-
- @Override
- public String toString() {
- return phoneNumber;
- }
- }
-
- private ArrayList<PhoneItem> makePhoneItemsList(Cursor phonesCursor) {
- ArrayList<PhoneItem> phoneList = new ArrayList<PhoneItem>();
-
- phonesCursor.moveToPosition(-1);
- while (phonesCursor.moveToNext()) {
- long id = phonesCursor.getLong(phonesCursor.getColumnIndex(Data._ID));
- String phone = phonesCursor.getString(phonesCursor.getColumnIndex(Phone.NUMBER));
- String accountType =
- phonesCursor.getString(phonesCursor.getColumnIndex(RawContacts.ACCOUNT_TYPE));
- int type = phonesCursor.getInt(phonesCursor.getColumnIndex(Phone.TYPE));
- String label = phonesCursor.getString(phonesCursor.getColumnIndex(Phone.LABEL));
-
- phoneList.add(new PhoneItem(id, phone, accountType, type, label));
- }
-
- return phoneList;
- }
-}
diff --git a/src/com/android/contacts/interactions/PhoneNumberInteraction.java b/src/com/android/contacts/interactions/PhoneNumberInteraction.java
new file mode 100644
index 0000000..5cf289e
--- /dev/null
+++ b/src/com/android/contacts/interactions/PhoneNumberInteraction.java
@@ -0,0 +1,365 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.contacts.interactions;
+
+
+import com.android.contacts.Collapser;
+import com.android.contacts.Collapser.Collapsible;
+import com.android.contacts.R;
+import com.android.contacts.model.ContactsSource;
+import com.android.contacts.model.ContactsSource.DataKind;
+import com.android.contacts.model.ContactsSource.StringInflater;
+import com.android.contacts.model.Sources;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.content.ContentUris;
+import android.content.ContentValues;
+import android.content.Context;
+import android.content.CursorLoader;
+import android.content.DialogInterface;
+import android.content.DialogInterface.OnClickListener;
+import android.content.DialogInterface.OnDismissListener;
+import android.content.Intent;
+import android.content.Loader;
+import android.content.Loader.OnLoadCompleteListener;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.Parcel;
+import android.os.Parcelable;
+import android.provider.ContactsContract.CommonDataKinds.Phone;
+import android.provider.ContactsContract.Contacts;
+import android.provider.ContactsContract.Data;
+import android.provider.ContactsContract.RawContacts;
+import android.telephony.PhoneNumberUtils;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.CheckBox;
+import android.widget.TextView;
+
+import java.util.ArrayList;
+
+/**
+ * Initiates phone calls or a text message.
+ */
+public class PhoneNumberInteraction
+ implements OnLoadCompleteListener<Cursor>, OnClickListener {
+
+ public static final String EXTRA_KEY_ITEMS = "items";
+
+ /**
+ * A model object for capturing a phone number for a given contact.
+ */
+ static class PhoneItem implements Parcelable, Collapsible<PhoneItem> {
+ long id;
+ String phoneNumber;
+ String accountType;
+ long type;
+ String label;
+
+ public static Parcelable.Creator<PhoneItem> CREATOR = new Creator<PhoneItem>() {
+
+ public PhoneItem[] newArray(int size) {
+ return new PhoneItem[size];
+ }
+
+ public PhoneItem createFromParcel(Parcel source) {
+ PhoneItem item = new PhoneItem();
+ item.id = source.readLong();
+ item.phoneNumber = source.readString();
+ item.accountType = source.readString();
+ item.type = source.readLong();
+ item.label = source.readString();
+ return item;
+ }
+ };
+
+ public void writeToParcel(Parcel dest, int flags) {
+ dest.writeLong(id);
+ dest.writeString(phoneNumber);
+ dest.writeString(accountType);
+ dest.writeLong(type);
+ dest.writeString(label);
+ }
+
+ public int describeContents() {
+ return 0;
+ }
+
+ public boolean collapseWith(PhoneItem phoneItem) {
+ if (!shouldCollapseWith(phoneItem)) {
+ return false;
+ }
+ // Just keep the number and id we already have.
+ return true;
+ }
+
+ public boolean shouldCollapseWith(PhoneItem phoneItem) {
+ if (PhoneNumberUtils.compareStrictly(phoneNumber, phoneItem.phoneNumber)) {
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return phoneNumber;
+ }
+ }
+
+ /**
+ * A list adapter that populates the list of contact's phone numbers.
+ */
+ private class PhoneItemAdapter extends ArrayAdapter<PhoneItem> {
+ private final Sources mSources;
+
+ public PhoneItemAdapter(Context context) {
+ super(context, R.layout.phone_disambig_item, android.R.id.text2);
+ mSources = Sources.getInstance(context);
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ View view = super.getView(position, convertView, parent);
+
+ PhoneItem item = getItem(position);
+ ContactsSource source = mSources.getInflatedSource(item.accountType,
+ ContactsSource.LEVEL_SUMMARY);
+
+ // Obtain a string representation of the phone type specific to the
+ // ContactSource associated with that phone number
+ TextView typeView = (TextView)view.findViewById(android.R.id.text1);
+ DataKind kind = source.getKindForMimetype(Phone.CONTENT_ITEM_TYPE);
+ if (kind != null) {
+ ContentValues values = new ContentValues();
+ values.put(Phone.TYPE, item.type);
+ values.put(Phone.LABEL, item.label);
+ StringInflater header = mSendTextMessage ? kind.actionAltHeader : kind.actionHeader;
+ typeView.setText(header.inflateUsing(getContext(), values));
+ } else {
+ typeView.setText(R.string.call_other);
+ }
+ return view;
+ }
+ }
+
+ private static final String[] PHONE_NUMBER_PROJECTION = new String[] {
+ Phone._ID,
+ Phone.NUMBER,
+ Phone.IS_SUPER_PRIMARY,
+ RawContacts.ACCOUNT_TYPE,
+ Phone.TYPE,
+ Phone.LABEL
+ };
+
+ private static final String PHONE_NUMBER_SELECTION = Data.MIMETYPE + "='"
+ + Phone.CONTENT_ITEM_TYPE + "' AND " + Phone.NUMBER + " NOT NULL";
+
+ private final Context mContext;
+ private final OnDismissListener mDismissListener;
+ private final boolean mSendTextMessage;
+
+ private CursorLoader mLoader;
+
+ public PhoneNumberInteraction(Context context, boolean sendTextMessage,
+ DialogInterface.OnDismissListener dismissListener) {
+ mContext = context;
+ mSendTextMessage = sendTextMessage;
+ mDismissListener = dismissListener;
+ }
+
+ private void performAction(String phoneNumber) {
+ Intent intent;
+ if (mSendTextMessage) {
+ intent = new Intent(
+ Intent.ACTION_SENDTO, Uri.fromParts("sms", phoneNumber, null));
+ } else {
+ intent = new Intent(
+ Intent.ACTION_CALL_PRIVILEGED, Uri.fromParts("tel", phoneNumber, null));
+
+ }
+ startActivity(intent);
+ }
+
+ /**
+ * Initiates the interaction. This may result in a phone call or sms message started
+ * or a disambiguation dialog to determine which phone number should be used.
+ */
+ public void startInteraction(Uri contactUri) {
+ if (mLoader != null) {
+ mLoader.destroy();
+ }
+
+ mLoader = new CursorLoader(mContext,
+ Uri.withAppendedPath(contactUri, Contacts.Data.CONTENT_DIRECTORY),
+ PHONE_NUMBER_PROJECTION,
+ PHONE_NUMBER_SELECTION,
+ null,
+ null);
+ mLoader.registerListener(0, this);
+ startLoading(mLoader);
+ }
+
+ @Override
+ public void onLoadComplete(Loader<Cursor> loader, Cursor cursor) {
+ if (cursor == null) {
+ onDismiss();
+ return;
+ }
+
+ ArrayList<PhoneItem> phoneList = new ArrayList<PhoneItem>();
+ String primaryPhone = null;
+ try {
+ while (cursor.moveToNext()) {
+ if (cursor.getInt(cursor.getColumnIndex(Phone.IS_SUPER_PRIMARY)) != 0) {
+ // Found super primary, call it.
+ primaryPhone = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
+ break;
+ }
+
+ PhoneItem item = new PhoneItem();
+ item.id = cursor.getLong(cursor.getColumnIndex(Data._ID));
+ item.phoneNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
+ item.accountType =
+ cursor.getString(cursor.getColumnIndex(RawContacts.ACCOUNT_TYPE));
+ item.type = cursor.getInt(cursor.getColumnIndex(Phone.TYPE));
+ item.label = cursor.getString(cursor.getColumnIndex(Phone.LABEL));
+
+ phoneList.add(item);
+ }
+ } finally {
+ cursor.close();
+ }
+
+ if (primaryPhone != null) {
+ performAction(primaryPhone);
+ onDismiss();
+ return;
+ }
+
+ Collapser.collapseList(phoneList);
+
+ if (phoneList.size() == 0) {
+ onDismiss();
+ } else if (phoneList.size() == 1) {
+ onDismiss();
+ performAction(phoneList.get(0).phoneNumber);
+ } else {
+ Bundle bundle = new Bundle();
+ bundle.putParcelableArrayList(EXTRA_KEY_ITEMS, phoneList);
+ showDialog(getDialogId(), bundle);
+ }
+ }
+
+ private void onDismiss() {
+ if (mDismissListener != null) {
+ mDismissListener.onDismiss(null);
+ }
+ }
+
+ private int getDialogId() {
+ return mSendTextMessage
+ ? R.id.dialog_phone_number_message_disambiguation
+ : R.id.dialog_phone_number_call_disambiguation;
+ }
+
+ public Dialog onCreateDialog(int id, Bundle bundle) {
+ if (id != getDialogId()) {
+ return null;
+ }
+
+ LayoutInflater inflater = LayoutInflater.from(mContext);
+ View setPrimaryView = inflater.inflate(R.layout.set_primary_checkbox, null);
+ AlertDialog dialog = new AlertDialog.Builder(mContext)
+ .setAdapter(new PhoneItemAdapter(mContext), this)
+ .setView(setPrimaryView)
+ .setTitle(mSendTextMessage
+ ? R.string.sms_disambig_title
+ : R.string.call_disambig_title)
+ .create();
+ dialog.setOnDismissListener(mDismissListener);
+ return dialog;
+ }
+
+ public boolean onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
+ if (id != getDialogId()) {
+ return false;
+ }
+
+ ArrayList<PhoneItem> phoneList = bundle.getParcelableArrayList(EXTRA_KEY_ITEMS);
+
+ AlertDialog alertDialog = (AlertDialog)dialog;
+ PhoneItemAdapter adapter = (PhoneItemAdapter)alertDialog.getListView().getAdapter();
+ adapter.clear();
+ adapter.addAll(phoneList);
+
+ return true;
+ }
+
+ /**
+ * Handles the user selection in the disambiguation dialog.
+ */
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ AlertDialog alertDialog = (AlertDialog)dialog;
+ PhoneItemAdapter adapter = (PhoneItemAdapter)alertDialog.getListView().getAdapter();
+ PhoneItem phoneItem = adapter.getItem(which);
+ if (phoneItem != null) {
+ long id = phoneItem.id;
+ String phone = phoneItem.phoneNumber;
+
+ CheckBox checkBox = (CheckBox)alertDialog.findViewById(R.id.setPrimary);
+ if (checkBox.isChecked()) {
+ makePrimary(id);
+ }
+
+ performAction(phone);
+ }
+ }
+
+ /**
+ * Makes the selected phone number primary.
+ */
+ void makePrimary(long id) {
+ // TODO use a Saver
+ ContentValues values = new ContentValues(1);
+ values.put(Data.IS_SUPER_PRIMARY, 1);
+ Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
+ mContext.getContentResolver().update(uri, values, null, null);
+ }
+
+ /* Visible for testing */
+ void showDialog(int dialogId, Bundle bundle) {
+ Activity activity = (Activity)mContext;
+ if (!activity.isFinishing()) {
+ activity.showDialog(dialogId, bundle);
+ }
+ }
+
+ /* Visible for testing */
+ void startActivity(Intent intent) {
+ mContext.startActivity(intent);
+ }
+
+ /* Visible for testing */
+ void startLoading(Loader<Cursor> loader) {
+ loader.startLoading();
+ }
+}
diff --git a/src/com/android/contacts/list/CallOrSmsInitiator.java b/src/com/android/contacts/list/CallOrSmsInitiator.java
deleted file mode 100644
index b5aabee..0000000
--- a/src/com/android/contacts/list/CallOrSmsInitiator.java
+++ /dev/null
@@ -1,139 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.contacts.list;
-
-import com.android.contacts.ContactsUtils;
-import com.android.contacts.PhoneDisambigDialog;
-import com.android.internal.widget.RotarySelector.OnDialTriggerListener;
-
-import android.content.AsyncQueryHandler;
-import android.content.Context;
-import android.content.DialogInterface;
-import android.content.DialogInterface.OnDismissListener;
-import android.database.Cursor;
-import android.net.Uri;
-import android.provider.ContactsContract.Contacts;
-import android.provider.ContactsContract.RawContacts;
-import android.provider.ContactsContract.CommonDataKinds.Phone;
-import android.provider.ContactsContract.Contacts.Data;
-
-/**
- * Initiates phone calls or SMS messages.
- */
-public class CallOrSmsInitiator {
-
- private final Context mContext;
- private AsyncQueryHandler mQueryHandler;
- private int mCurrentToken;
- private boolean mSendSms;
-
- private static final String[] PHONE_NUMBER_PROJECTION = new String[] {
- Phone._ID,
- Phone.NUMBER,
- Phone.IS_SUPER_PRIMARY,
- RawContacts.ACCOUNT_TYPE,
- Phone.TYPE,
- Phone.LABEL
- };
-
- private static final String PHONE_NUMBER_SELECTION = Data.MIMETYPE + "='"
- + Phone.CONTENT_ITEM_TYPE + "' AND " + Phone.NUMBER + " NOT NULL";
- private OnDismissListener mDismissListener;
-
- public CallOrSmsInitiator(Context context) {
- this.mContext = context;
- mQueryHandler = new AsyncQueryHandler(context.getContentResolver()) {
- @Override
- protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
- onPhoneNumberQueryComplete(token, cookie, cursor);
- }
- };
- }
-
- public void setOnDismissListener(DialogInterface.OnDismissListener dismissListener) {
- this.mDismissListener = dismissListener;
- }
-
- protected void onPhoneNumberQueryComplete(int token, Object cookie, Cursor cursor) {
- if (cursor == null || cursor.getCount() == 0) {
- cursor.close();
- return;
- }
-
- if (token != mCurrentToken) { // Stale query, ignore
- cursor.close();
- return;
- }
-
- String phone = null;
- if (cursor.getCount() == 1) {
- // only one number, call it.
- cursor.moveToFirst();
- phone = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
- } else {
- cursor.moveToPosition(-1);
- while (cursor.moveToNext()) {
- if (cursor.getInt(cursor.getColumnIndex(Phone.IS_SUPER_PRIMARY)) != 0) {
- // Found super primary, call it.
- phone = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
- break;
- }
- }
- }
-
- if (phone == null) {
- // Display dialog to choose a number to call.
- PhoneDisambigDialog phoneDialog = new PhoneDisambigDialog(mContext, cursor, mSendSms);
- if (mDismissListener != null) {
- phoneDialog.setOnDismissListener(mDismissListener);
- }
- phoneDialog.show();
- } else {
- if (mSendSms) {
- ContactsUtils.initiateSms(mContext, phone);
- } else {
- ContactsUtils.initiateCall(mContext, phone);
- }
- if (mDismissListener != null) {
- mDismissListener.onDismiss(null);
- }
- }
- }
-
- /**
- * Initiates a phone call with the specified contact. If necessary, displays
- * a disambiguation dialog to see which number to call.
- */
- public void initiateCall(Uri contactUri) {
- callOrSendSms(contactUri, false);
- }
-
- /**
- * Initiates a text message to the specified contact. If necessary, displays
- * a disambiguation dialog to see which number to call.
- */
- public void initiateSms(Uri contactUri) {
- callOrSendSms(contactUri, true);
- }
-
- private void callOrSendSms(Uri contactUri, boolean sendSms) {
- mCurrentToken++;
- mSendSms = sendSms;
- Uri dataUri = Uri.withAppendedPath(contactUri, Contacts.Data.CONTENT_DIRECTORY);
- mQueryHandler.startQuery(mCurrentToken, dataUri, dataUri, PHONE_NUMBER_PROJECTION,
- PHONE_NUMBER_SELECTION, null, null);
- }
-}
diff --git a/tests/src/com/android/contacts/interactions/PhoneNumberInteractionTest.java b/tests/src/com/android/contacts/interactions/PhoneNumberInteractionTest.java
new file mode 100644
index 0000000..8c6a1ac
--- /dev/null
+++ b/tests/src/com/android/contacts/interactions/PhoneNumberInteractionTest.java
@@ -0,0 +1,184 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts.interactions;
+
+import com.android.contacts.R;
+import com.android.contacts.interactions.PhoneNumberInteraction.PhoneItem;
+import com.android.contacts.tests.mocks.ContactsMockContext;
+import com.android.contacts.tests.mocks.MockContentProvider;
+import com.android.contacts.tests.mocks.MockContentProvider.Query;
+
+import android.content.AsyncTaskLoader;
+import android.content.ContentUris;
+import android.content.Context;
+import android.content.DialogInterface.OnDismissListener;
+import android.content.Intent;
+import android.content.Loader;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.Parcelable;
+import android.provider.ContactsContract.CommonDataKinds.Phone;
+import android.provider.ContactsContract.Contacts;
+import android.provider.ContactsContract.RawContacts;
+import android.test.InstrumentationTestCase;
+import android.test.suitebuilder.annotation.Smoke;
+
+import java.util.ArrayList;
+
+/**
+ * Tests for {@link PhoneNumberInteraction}.
+ *
+ * Running all tests:
+ *
+ * runtest contacts
+ * or
+ * adb shell am instrument \
+ * -w com.android.contacts.tests/android.test.InstrumentationTestRunner
+ */
+@Smoke
+public class PhoneNumberInteractionTest extends InstrumentationTestCase {
+
+ private final static class TestPhoneNumberInteraction extends PhoneNumberInteraction {
+ Intent startedIntent;
+ int dialogId;
+ Bundle dialogArgs;
+
+ public TestPhoneNumberInteraction(
+ Context context, boolean sendTextMessage, OnDismissListener dismissListener) {
+ super(context, sendTextMessage, dismissListener);
+ }
+
+ @Override
+ void startLoading(Loader<Cursor> loader) {
+ // Execute the loader synchronously
+ AsyncTaskLoader<Cursor> atLoader = (AsyncTaskLoader<Cursor>)loader;
+ Cursor data = atLoader.loadInBackground();
+ atLoader.deliverResult(data);
+ }
+
+ @Override
+ void startActivity(Intent intent) {
+ this.startedIntent = intent;
+ }
+
+ @Override
+ void showDialog(int dialogId, Bundle bundle) {
+ this.dialogId = dialogId;
+ this.dialogArgs = bundle;
+ }
+ }
+
+ private ContactsMockContext mContext;
+ private MockContentProvider mContactsProvider;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mContext = new ContactsMockContext(getInstrumentation().getTargetContext());
+ mContactsProvider = mContext.getContactsProvider();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ mContactsProvider.verify();
+ super.tearDown();
+ }
+
+ public void testSendSmsWhenOnlyOneNumberAvailable() {
+ Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
+ expectQuery(contactUri)
+ .returnRow(1, "123", 0, null, Phone.TYPE_HOME, null);
+
+ TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
+ mContext, true, null);
+
+ interaction.startInteraction(contactUri);
+
+ assertEquals(Intent.ACTION_SENDTO, interaction.startedIntent.getAction());
+ assertEquals("sms:123", interaction.startedIntent.getDataString());
+ }
+
+ public void testSendSmsWhenThereIsPrimaryNumber() {
+ Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
+ expectQuery(contactUri)
+ .returnRow(1, "123", 0, null, Phone.TYPE_HOME, null)
+ .returnRow(2, "456", 1, null, Phone.TYPE_HOME, null);
+
+ TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
+ mContext, true, null);
+
+ interaction.startInteraction(contactUri);
+
+ assertEquals(Intent.ACTION_SENDTO, interaction.startedIntent.getAction());
+ assertEquals("sms:456", interaction.startedIntent.getDataString());
+ }
+
+ public void testCallNumberWhenThereAreDuplicates() {
+ Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
+ expectQuery(contactUri)
+ .returnRow(1, "123", 0, null, Phone.TYPE_HOME, null)
+ .returnRow(2, "123", 0, null, Phone.TYPE_WORK, null);
+
+ TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
+ mContext, false, null);
+
+ interaction.startInteraction(contactUri);
+
+ assertEquals(Intent.ACTION_CALL_PRIVILEGED, interaction.startedIntent.getAction());
+ assertEquals("tel:123", interaction.startedIntent.getDataString());
+ }
+
+ public void testShowDisambigDialogForCalling() {
+ Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
+ expectQuery(contactUri)
+ .returnRow(1, "123", 0, "account", Phone.TYPE_HOME, "label")
+ .returnRow(2, "456", 0, null, Phone.TYPE_WORK, null);
+
+ TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
+ mContext, false, null);
+
+ interaction.startInteraction(contactUri);
+
+ assertEquals(R.id.dialog_phone_number_call_disambiguation, interaction.dialogId);
+
+ ArrayList<PhoneItem> items = interaction.dialogArgs.getParcelableArrayList(
+ PhoneNumberInteraction.EXTRA_KEY_ITEMS);
+ assertEquals(2, items.size());
+
+ PhoneItem item = items.get(0);
+ assertEquals(1, item.id);
+ assertEquals("123", item.phoneNumber);
+ assertEquals("account", item.accountType);
+ assertEquals(Phone.TYPE_HOME, item.type);
+ assertEquals("label", item.label);
+ }
+
+ private Query expectQuery(Uri contactUri) {
+ Uri dataUri = Uri.withAppendedPath(contactUri, Contacts.Data.CONTENT_DIRECTORY);
+ return mContactsProvider
+ .expectQuery(dataUri)
+ .withProjection(
+ Phone._ID,
+ Phone.NUMBER,
+ Phone.IS_SUPER_PRIMARY,
+ RawContacts.ACCOUNT_TYPE,
+ Phone.TYPE,
+ Phone.LABEL)
+ .withSelection("mimetype='vnd.android.cursor.item/phone_v2' AND data1 NOT NULL");
+ }
+}