blob: 181fb37ab6cec32dca90296552be81c63b79afac [file] [log] [blame]
/*
* 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.util.Constants;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.location.CountryDetector;
import android.net.Uri;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Data;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
public class ContactsUtils {
private static final String TAG = "ContactsUtils";
private static final String WAIT_SYMBOL_AS_STRING = String.valueOf(PhoneNumberUtils.WAIT);
// TODO find a proper place for the canonical version of these
public interface ProviderNames {
String YAHOO = "Yahoo";
String GTALK = "GTalk";
String MSN = "MSN";
String ICQ = "ICQ";
String AIM = "AIM";
String XMPP = "XMPP";
String JABBER = "JABBER";
String SKYPE = "SKYPE";
String QQ = "QQ";
}
/**
* This looks up the provider name defined in
* ProviderNames from the predefined IM protocol id.
* This is used for interacting with the IM application.
*
* @param protocol the protocol ID
* @return the provider name the IM app uses for the given protocol, or null if no
* provider is defined for the given protocol
* @hide
*/
public static String lookupProviderNameFromId(int protocol) {
switch (protocol) {
case Im.PROTOCOL_GOOGLE_TALK:
return ProviderNames.GTALK;
case Im.PROTOCOL_AIM:
return ProviderNames.AIM;
case Im.PROTOCOL_MSN:
return ProviderNames.MSN;
case Im.PROTOCOL_YAHOO:
return ProviderNames.YAHOO;
case Im.PROTOCOL_ICQ:
return ProviderNames.ICQ;
case Im.PROTOCOL_JABBER:
return ProviderNames.JABBER;
case Im.PROTOCOL_SKYPE:
return ProviderNames.SKYPE;
case Im.PROTOCOL_QQ:
return ProviderNames.QQ;
}
return null;
}
public static final class ImActions {
private final Intent mPrimaryIntent;
private final Intent mSecondaryIntent;
private final int mPrimaryActionIcon;
private final int mSecondaryActionIcon;
private ImActions(Intent primaryIntent, Intent secondaryIntent, int primaryActionIcon,
int secondaryActionIcon) {
mPrimaryIntent = primaryIntent;
mSecondaryIntent = secondaryIntent;
mPrimaryActionIcon = primaryActionIcon;
mSecondaryActionIcon = secondaryActionIcon;
}
public Intent getPrimaryIntent() {
return mPrimaryIntent;
}
public Intent getSecondaryIntent() {
return mSecondaryIntent;
}
public int getPrimaryActionIcon() {
return mPrimaryActionIcon;
}
public int getSecondaryActionIcon() {
return mSecondaryActionIcon;
}
}
/**
* Build {@link Intent} to launch an action for the given {@link Im} or
* {@link Email} row. If the result is non-null, it either contains one or two Intents
* (e.g. [Text, Videochat] or just [Text])
* Returns null when missing protocol or data.
*/
public static ImActions buildImActions(ContentValues values) {
final boolean isEmail = Email.CONTENT_ITEM_TYPE.equals(values.getAsString(Data.MIMETYPE));
if (!isEmail && !isProtocolValid(values)) {
return null;
}
final String data = values.getAsString(isEmail ? Email.DATA : Im.DATA);
if (TextUtils.isEmpty(data)) return null;
final int protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : values.getAsInteger(Im.PROTOCOL);
if (protocol == Im.PROTOCOL_GOOGLE_TALK) {
final Integer chatCapabilityObj = values.getAsInteger(Im.CHAT_CAPABILITY);
final int chatCapability = chatCapabilityObj == null ? 0 : chatCapabilityObj;
if ((chatCapability & Im.CAPABILITY_HAS_CAMERA) != 0) {
// Allow Video chat and Texting
return new ImActions(
new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message")),
new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?call")),
R.drawable.sym_action_talk_holo_light,
R.drawable.sym_action_videochat
);
} else if ((chatCapability & Im.CAPABILITY_HAS_VOICE) != 0) {
// Allow Talking and Texting
return new ImActions(
new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message")),
new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?call")),
R.drawable.sym_action_talk_holo_light,
R.drawable.sym_action_audiochat
);
} else {
return new ImActions(
new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message")),
null,
R.drawable.sym_action_talk_holo_light,
-1
);
}
} else {
// Build an IM Intent
String host = values.getAsString(Im.CUSTOM_PROTOCOL);
if (protocol != Im.PROTOCOL_CUSTOM) {
// Try bringing in a well-known host for specific protocols
host = ContactsUtils.lookupProviderNameFromId(protocol);
}
if (!TextUtils.isEmpty(host)) {
final String authority = host.toLowerCase();
final Uri imUri = new Uri.Builder().scheme(Constants.SCHEME_IMTO).authority(
authority).appendPath(data).build();
return new ImActions(
new Intent(Intent.ACTION_SENDTO, imUri),
null,
R.drawable.sym_action_talk_holo_light,
-1
);
} else {
return null;
}
}
}
private static boolean isProtocolValid(ContentValues values) {
String protocolString = values.getAsString(Im.PROTOCOL);
if (protocolString == null) {
return false;
}
try {
Integer.valueOf(protocolString);
} catch (NumberFormatException e) {
return false;
}
return true;
}
/**
* Test if the given {@link CharSequence} contains any graphic characters,
* first checking {@link TextUtils#isEmpty(CharSequence)} to handle null.
*/
public static boolean isGraphic(CharSequence str) {
return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str);
}
/**
* Returns true if two objects are considered equal. Two null references are equal here.
*/
public static boolean areObjectsEqual(Object a, Object b) {
return a == b || (a != null && a.equals(b));
}
/**
* Returns true if two data with mimetypes which represent values in contact entries are
* considered equal for collapsing in the GUI. For caller-id, use
* {@link PhoneNumberUtils#compare(Context, String, String)} instead
*/
public static final boolean shouldCollapse(Context context, CharSequence mimetype1,
CharSequence data1, CharSequence mimetype2, CharSequence data2) {
if (TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype1)
&& TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype2)) {
if (data1 == data2) {
return true;
}
if (data1 == null || data2 == null) {
return false;
}
// If the number contains semicolons, PhoneNumberUtils.compare
// only checks the substring before that (which is fine for caller-id usually)
// but not for collapsing numbers. so we check each segment indidually to be more strict
// TODO: This should be replaced once we have a more robust phonenumber-library
String[] dataParts1 = data1.toString().split(WAIT_SYMBOL_AS_STRING);
String[] dataParts2 = data2.toString().split(WAIT_SYMBOL_AS_STRING);
if (dataParts1.length != dataParts2.length) {
return false;
}
for (int i = 0; i < dataParts1.length; i++) {
if (!PhoneNumberUtils.compare(context, dataParts1[i], dataParts2[i])) {
return false;
}
}
return true;
} else {
if (mimetype1 == mimetype2 && data1 == data2) {
return true;
}
return TextUtils.equals(mimetype1, mimetype2) && TextUtils.equals(data1, data2);
}
}
/**
* Returns true if two {@link Intent}s are both null, or have the same action.
*/
public static final boolean areIntentActionEqual(Intent a, Intent b) {
if (a == b) {
return true;
}
if (a == null || b == null) {
return false;
}
return TextUtils.equals(a.getAction(), b.getAction());
}
/**
* @return The ISO 3166-1 two letters country code of the country the user
* is in.
*/
public static final String getCurrentCountryIso(Context context) {
CountryDetector detector =
(CountryDetector) context.getSystemService(Context.COUNTRY_DETECTOR);
return detector.detectCountry().getCountryIso();
}
}