diff options
-rw-r--r-- | src/com/android/contacts/common/ContactsUtils.java | 21 | ||||
-rw-r--r-- | tests/src/com/android/contacts/common/ContactsUtilsTests.java | 34 |
2 files changed, 52 insertions, 3 deletions
diff --git a/src/com/android/contacts/common/ContactsUtils.java b/src/com/android/contacts/common/ContactsUtils.java index b30bbd66..a133b5bd 100644 --- a/src/com/android/contacts/common/ContactsUtils.java +++ b/src/com/android/contacts/common/ContactsUtils.java @@ -19,12 +19,14 @@ package com.android.contacts.common; import android.content.Context; import android.content.Intent; import android.database.Cursor; +import android.net.Uri; import android.provider.ContactsContract.CommonDataKinds.Im; import android.provider.ContactsContract.DisplayPhoto; import android.telephony.PhoneNumberUtils; import android.text.TextUtils; import com.android.contacts.common.model.account.AccountWithDataSet; +import com.android.contacts.common.model.dataitem.ImDataItem; import com.android.contacts.common.testing.NeededForTesting; import com.android.contacts.common.model.AccountTypeManager; @@ -141,4 +143,23 @@ public class ContactsUtils { return sThumbnailSize; } + public static Intent getCustomIMIntent(ImDataItem im, int protocol) { + String host = im.getCustomProtocol(); + final String data = im.getData(); + if (TextUtils.isEmpty(data)) { + return null; + } + if (protocol != Im.PROTOCOL_CUSTOM) { + // Try bringing in a well-known host for specific protocols + host = ContactsUtils.lookupProviderNameFromId(protocol); + } + if (TextUtils.isEmpty(host)) { + return null; + } + final String authority = host.toLowerCase(); + final Uri imUri = new Uri.Builder().scheme(CallUtil.SCHEME_IMTO).authority( + authority).appendPath(data).build(); + final Intent intent = new Intent(Intent.ACTION_SENDTO, imUri); + return intent; + } } diff --git a/tests/src/com/android/contacts/common/ContactsUtilsTests.java b/tests/src/com/android/contacts/common/ContactsUtilsTests.java index c0df3dd4..eeaf1211 100644 --- a/tests/src/com/android/contacts/common/ContactsUtilsTests.java +++ b/tests/src/com/android/contacts/common/ContactsUtilsTests.java @@ -14,15 +14,18 @@ * limitations under the License. */ -package src.com.android.contacts.common; +package com.android.contacts.common; +import android.content.ContentValues; import android.content.Intent; -import android.provider.ContactsContract.CommonDataKinds.Phone; +import android.net.Uri; +import android.provider.ContactsContract.CommonDataKinds.Im; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.SmallTest; import com.android.contacts.common.ContactsUtils; -import com.android.contacts.common.MoreContactUtils; +import com.android.contacts.common.model.dataitem.DataItem; +import com.android.contacts.common.model.dataitem.ImDataItem; /** * Tests for {@link ContactsUtils}. @@ -66,4 +69,29 @@ public class ContactsUtilsTests extends AndroidTestCase { assertFalse("22", ContactsUtils.areIntentActionEqual(new Intent(), new Intent("b"))); assertFalse("23", ContactsUtils.areIntentActionEqual(new Intent("a"), new Intent("b"))); } + + public void testImIntentCustom() throws Exception { + final String testAddress = "user@example.org"; + final String testProtocol = "prot%col"; + + + // Custom IM types have encoded authority. We send the imto Intent here, because + // legacy third party apps might not accept xmpp yet + final ContentValues values = new ContentValues(); + values.put(Im.MIMETYPE, Im.CONTENT_ITEM_TYPE); + values.put(Im.TYPE, Im.TYPE_HOME); + values.put(Im.PROTOCOL, Im.PROTOCOL_CUSTOM); + values.put(Im.CUSTOM_PROTOCOL, testProtocol); + values.put(Im.DATA, testAddress); + ImDataItem im = (ImDataItem) DataItem.createFrom(values); + + final Intent imIntent = + ContactsUtils.getCustomIMIntent(im, Im.PROTOCOL_CUSTOM); + assertEquals(Intent.ACTION_SENDTO, imIntent.getAction()); + + final Uri data = imIntent.getData(); + assertEquals("imto", data.getScheme()); + assertEquals(testProtocol, data.getAuthority()); + assertEquals(testAddress, data.getPathSegments().get(0)); + } } |