diff options
Diffstat (limited to 'telecomm')
-rw-r--r-- | telecomm/java/android/telecom/CallerInfo.java | 782 | ||||
-rw-r--r-- | telecomm/java/android/telecom/CallerInfoAsyncQuery.java | 548 | ||||
-rw-r--r-- | telecomm/java/android/telecom/Connection.java | 6 | ||||
-rw-r--r-- | telecomm/java/android/telecom/TelecomManager.java | 3 |
4 files changed, 1335 insertions, 4 deletions
diff --git a/telecomm/java/android/telecom/CallerInfo.java b/telecomm/java/android/telecom/CallerInfo.java new file mode 100644 index 000000000000..a5d25e2ce4bb --- /dev/null +++ b/telecomm/java/android/telecom/CallerInfo.java @@ -0,0 +1,782 @@ +/* + * Copyright (C) 2006 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 android.telecom; + +import android.annotation.Nullable; +import android.annotation.UnsupportedAppUsage; +import android.content.ComponentName; +import android.content.ContentResolver; +import android.content.Context; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.drawable.Drawable; +import android.location.Country; +import android.location.CountryDetector; +import android.net.Uri; +import android.provider.ContactsContract.CommonDataKinds.Phone; +import android.provider.ContactsContract.Contacts; +import android.provider.ContactsContract.Data; +import android.provider.ContactsContract.PhoneLookup; +import android.provider.ContactsContract.RawContacts; +import android.telephony.PhoneNumberUtils; +import android.telephony.SubscriptionManager; +import android.telephony.TelephonyManager; +import android.text.TextUtils; + +import com.android.i18n.phonenumbers.NumberParseException; +import com.android.i18n.phonenumbers.PhoneNumberUtil; +import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber; +import com.android.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder; + +import com.android.internal.annotations.VisibleForTesting; +import java.util.Locale; + + +/** + * Looks up caller information for the given phone number. + * + * {@hide} + */ +public class CallerInfo { + private static final String TAG = "CallerInfo"; + private static final boolean VDBG = Log.VERBOSE; + + /** @hide */ + public static final long USER_TYPE_CURRENT = 0; + /** @hide */ + public static final long USER_TYPE_WORK = 1; + + /** + * Please note that, any one of these member variables can be null, + * and any accesses to them should be prepared to handle such a case. + * + * Also, it is implied that phoneNumber is more often populated than + * name is, (think of calls being dialed/received using numbers where + * names are not known to the device), so phoneNumber should serve as + * a dependable fallback when name is unavailable. + * + * One other detail here is that this CallerInfo object reflects + * information found on a connection, it is an OUTPUT that serves + * mainly to display information to the user. In no way is this object + * used as input to make a connection, so we can choose to display + * whatever human-readable text makes sense to the user for a + * connection. This is especially relevant for the phone number field, + * since it is the one field that is most likely exposed to the user. + * + * As an example: + * 1. User dials "911" + * 2. Device recognizes that this is an emergency number + * 3. We use the "Emergency Number" string instead of "911" in the + * phoneNumber field. + * + * What we're really doing here is treating phoneNumber as an essential + * field here, NOT name. We're NOT always guaranteed to have a name + * for a connection, but the number should be displayable. + */ + private String name; + private String phoneNumber; + /** @hide */ + public String normalizedNumber; + /** @hide */ + public String geoDescription; + /** @hide */ + public String cnapName; + /** @hide */ + public int numberPresentation; + /** @hide */ + public int namePresentation; + /** @hide */ + public boolean contactExists; + /** @hide */ + public String phoneLabel; + /** + * Split up the phoneLabel into number type and label name. + * @hide + */ + @UnsupportedAppUsage + public int numberType; + /** @hide */ + @UnsupportedAppUsage + public String numberLabel; + /** @hide */ + public int photoResource; + + // Contact ID, which will be 0 if a contact comes from the corp CP2. + private long contactIdOrZero; + /** @hide */ + public boolean needUpdate; + /** @hide */ + public Uri contactRefUri; + /** @hide */ + public String lookupKey; + /** @hide */ + public ComponentName preferredPhoneAccountComponent; + /** @hide */ + public String preferredPhoneAccountId; + /** @hide */ + public long userType; + + /** + * Contact display photo URI. If a contact has no display photo but a thumbnail, it'll be + * the thumbnail URI instead. + */ + private Uri contactDisplayPhotoUri; + + // fields to hold individual contact preference data, + // including the send to voicemail flag and the ringtone + // uri reference. + /** @hide */ + public Uri contactRingtoneUri; + /** @hide */ + public boolean shouldSendToVoicemail; + + /** + * Drawable representing the caller image. This is essentially + * a cache for the image data tied into the connection / + * callerinfo object. + * + * This might be a high resolution picture which is more suitable + * for full-screen image view than for smaller icons used in some + * kinds of notifications. + * + * The {@link #isCachedPhotoCurrent} flag indicates if the image + * data needs to be reloaded. + * + * @hide + */ + public Drawable cachedPhoto; + /** + * Bitmap representing the caller image which has possibly lower + * resolution than {@link #cachedPhoto} and thus more suitable for + * icons (like notification icons). + * + * In usual cases this is just down-scaled image of {@link #cachedPhoto}. + * If the down-scaling fails, this will just become null. + * + * The {@link #isCachedPhotoCurrent} flag indicates if the image + * data needs to be reloaded. + * + * @hide + */ + public Bitmap cachedPhotoIcon; + /** + * Boolean which indicates if {@link #cachedPhoto} and + * {@link #cachedPhotoIcon} is fresh enough. If it is false, + * those images aren't pointing to valid objects. + * + * @hide + */ + public boolean isCachedPhotoCurrent; + + private boolean mIsEmergency; + private boolean mIsVoiceMail; + + /** @hide */ + @UnsupportedAppUsage + public CallerInfo() { + // TODO: Move all the basic initialization here? + mIsEmergency = false; + mIsVoiceMail = false; + userType = USER_TYPE_CURRENT; + } + + /** + * getCallerInfo given a Cursor. + * @param context the context used to retrieve string constants + * @param contactRef the URI to attach to this CallerInfo object + * @param cursor the first object in the cursor is used to build the CallerInfo object. + * @return the CallerInfo which contains the caller id for the given + * number. The returned CallerInfo is null if no number is supplied. + * + * @hide + */ + public static CallerInfo getCallerInfo(Context context, Uri contactRef, Cursor cursor) { + CallerInfo info = new CallerInfo(); + info.photoResource = 0; + info.phoneLabel = null; + info.numberType = 0; + info.numberLabel = null; + info.cachedPhoto = null; + info.isCachedPhotoCurrent = false; + info.contactExists = false; + info.userType = USER_TYPE_CURRENT; + + if (VDBG) Log.v(TAG, "getCallerInfo() based on cursor..."); + + if (cursor != null) { + if (cursor.moveToFirst()) { + // TODO: photo_id is always available but not taken + // care of here. Maybe we should store it in the + // CallerInfo object as well. + + int columnIndex; + + // Look for the name + columnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME); + if (columnIndex != -1) { + info.name = cursor.getString(columnIndex); + } + + // Look for the number + columnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER); + if (columnIndex != -1) { + info.phoneNumber = cursor.getString(columnIndex); + } + + // Look for the normalized number + columnIndex = cursor.getColumnIndex(PhoneLookup.NORMALIZED_NUMBER); + if (columnIndex != -1) { + info.normalizedNumber = cursor.getString(columnIndex); + } + + // Look for the label/type combo + columnIndex = cursor.getColumnIndex(PhoneLookup.LABEL); + if (columnIndex != -1) { + int typeColumnIndex = cursor.getColumnIndex(PhoneLookup.TYPE); + if (typeColumnIndex != -1) { + info.numberType = cursor.getInt(typeColumnIndex); + info.numberLabel = cursor.getString(columnIndex); + info.phoneLabel = Phone.getDisplayLabel(context, + info.numberType, info.numberLabel) + .toString(); + } + } + + // Look for the person_id. + columnIndex = getColumnIndexForPersonId(contactRef, cursor); + if (columnIndex != -1) { + final long contactId = cursor.getLong(columnIndex); + if (contactId != 0 && !Contacts.isEnterpriseContactId(contactId)) { + info.contactIdOrZero = contactId; + if (VDBG) { + Log.v(TAG, "==> got info.contactIdOrZero: " + info.contactIdOrZero); + } + } + if (Contacts.isEnterpriseContactId(contactId)) { + info.userType = USER_TYPE_WORK; + } + } else { + // No valid columnIndex, so we can't look up person_id. + Log.w(TAG, "Couldn't find contact_id column for " + contactRef); + // Watch out: this means that anything that depends on + // person_id will be broken (like contact photo lookups in + // the in-call UI, for example.) + } + + // Contact lookupKey + columnIndex = cursor.getColumnIndex(PhoneLookup.LOOKUP_KEY); + if (columnIndex != -1) { + info.lookupKey = cursor.getString(columnIndex); + } + + // Display photo URI. + columnIndex = cursor.getColumnIndex(PhoneLookup.PHOTO_URI); + if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) { + info.contactDisplayPhotoUri = Uri.parse(cursor.getString(columnIndex)); + } else { + info.contactDisplayPhotoUri = null; + } + + columnIndex = cursor.getColumnIndex(Data.PREFERRED_PHONE_ACCOUNT_COMPONENT_NAME); + if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) { + info.preferredPhoneAccountComponent = + ComponentName.unflattenFromString(cursor.getString(columnIndex)); + } + + columnIndex = cursor.getColumnIndex(Data.PREFERRED_PHONE_ACCOUNT_ID); + if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) { + info.preferredPhoneAccountId = cursor.getString(columnIndex); + } + + // look for the custom ringtone, create from the string stored + // in the database. + // An empty string ("") in the database indicates a silent ringtone, + // and we set contactRingtoneUri = Uri.EMPTY, so that no ringtone will be played. + // {null} in the database indicates the default ringtone, + // and we set contactRingtoneUri = null, so that default ringtone will be played. + columnIndex = cursor.getColumnIndex(PhoneLookup.CUSTOM_RINGTONE); + if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) { + if (TextUtils.isEmpty(cursor.getString(columnIndex))) { + info.contactRingtoneUri = Uri.EMPTY; + } else { + info.contactRingtoneUri = Uri.parse(cursor.getString(columnIndex)); + } + } else { + info.contactRingtoneUri = null; + } + + // look for the send to voicemail flag, set it to true only + // under certain circumstances. + columnIndex = cursor.getColumnIndex(PhoneLookup.SEND_TO_VOICEMAIL); + info.shouldSendToVoicemail = (columnIndex != -1) && + ((cursor.getInt(columnIndex)) == 1); + info.contactExists = true; + } + cursor.close(); + cursor = null; + } + + info.needUpdate = false; + info.name = normalize(info.name); + info.contactRefUri = contactRef; + + return info; + } + + /** + * getCallerInfo given a URI, look up in the call-log database + * for the uri unique key. + * @param context the context used to get the ContentResolver + * @param contactRef the URI used to lookup caller id + * @return the CallerInfo which contains the caller id for the given + * number. The returned CallerInfo is null if no number is supplied. + * + * @hide + */ + @UnsupportedAppUsage + public static CallerInfo getCallerInfo(Context context, Uri contactRef) { + CallerInfo info = null; + ContentResolver cr = CallerInfoAsyncQuery.getCurrentProfileContentResolver(context); + if (cr != null) { + try { + info = getCallerInfo(context, contactRef, + cr.query(contactRef, null, null, null, null)); + } catch (RuntimeException re) { + Log.e(TAG, re, "Error getting caller info."); + } + } + return info; + } + + /** + * getCallerInfo given a phone number, look up in the call-log database + * for the matching caller id info. + * @param context the context used to get the ContentResolver + * @param number the phone number used to lookup caller id + * @return the CallerInfo which contains the caller id for the given + * number. The returned CallerInfo is null if no number is supplied. If + * a matching number is not found, then a generic caller info is returned, + * with all relevant fields empty or null. + * + * @hide + */ + @UnsupportedAppUsage + public static CallerInfo getCallerInfo(Context context, String number) { + if (VDBG) Log.v(TAG, "getCallerInfo() based on number..."); + + int subId = SubscriptionManager.getDefaultSubscriptionId(); + return getCallerInfo(context, number, subId); + } + + /** + * getCallerInfo given a phone number and subscription, look up in the call-log database + * for the matching caller id info. + * @param context the context used to get the ContentResolver + * @param number the phone number used to lookup caller id + * @param subId the subscription for checking for if voice mail number or not + * @return the CallerInfo which contains the caller id for the given + * number. The returned CallerInfo is null if no number is supplied. If + * a matching number is not found, then a generic caller info is returned, + * with all relevant fields empty or null. + * + * @hide + */ + @UnsupportedAppUsage + public static CallerInfo getCallerInfo(Context context, String number, int subId) { + + if (TextUtils.isEmpty(number)) { + return null; + } + + // Change the callerInfo number ONLY if it is an emergency number + // or if it is the voicemail number. If it is either, take a + // shortcut and skip the query. + if (PhoneNumberUtils.isLocalEmergencyNumber(context, number)) { + return new CallerInfo().markAsEmergency(context); + } else if (PhoneNumberUtils.isVoiceMailNumber(null, subId, number)) { + return new CallerInfo().markAsVoiceMail(context, subId); + } + + Uri contactUri = Uri.withAppendedPath(PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI, + Uri.encode(number)); + + CallerInfo info = getCallerInfo(context, contactUri); + info = doSecondaryLookupIfNecessary(context, number, info); + + // if no query results were returned with a viable number, + // fill in the original number value we used to query with. + if (TextUtils.isEmpty(info.phoneNumber)) { + info.phoneNumber = number; + } + + return info; + } + + /** + * @return Name assocaited with this caller. + */ + @Nullable + public String getName() { + return name; + } + + /** + * Set caller Info Name. + * @param name caller Info Name + * + * @hide + */ + public void setName(@Nullable String name) { + this.name = name; + } + + /** + * @return Phone number assocaited with this caller. + */ + @Nullable + public String getPhoneNumber() { + return phoneNumber; + } + + /** @hide */ + public void setPhoneNumber(String number) { + phoneNumber = number; + } + + /** + * @return Contact ID, which will be 0 if a contact comes from the corp Contacts Provider. + */ + public long getContactId() { + return contactIdOrZero; + } + + /** + * @return Contact display photo URI. If a contact has no display photo but a thumbnail, + * it'll the thumbnail URI instead. + */ + @Nullable + public Uri getContactDisplayPhotoUri() { + return contactDisplayPhotoUri; + } + + /** @hide */ + @VisibleForTesting + public void SetContactDisplayPhotoUri(Uri photoUri) { + contactDisplayPhotoUri = photoUri; + } + + /** + * Performs another lookup if previous lookup fails and it's a SIP call + * and the peer's username is all numeric. Look up the username as it + * could be a PSTN number in the contact database. + * + * @param context the query context + * @param number the original phone number, could be a SIP URI + * @param previousResult the result of previous lookup + * @return previousResult if it's not the case + */ + static CallerInfo doSecondaryLookupIfNecessary(Context context, + String number, CallerInfo previousResult) { + if (!previousResult.contactExists + && PhoneNumberUtils.isUriNumber(number)) { + String username = PhoneNumberUtils.getUsernameFromUriNumber(number); + if (PhoneNumberUtils.isGlobalPhoneNumber(username)) { + previousResult = getCallerInfo(context, + Uri.withAppendedPath(PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI, + Uri.encode(username))); + } + } + return previousResult; + } + + // Accessors + + /** + * @return true if the caller info is an emergency number. + * @hide + */ + public boolean isEmergencyNumber() { + return mIsEmergency; + } + + /** + * @return true if the caller info is a voicemail number. + * @hide + */ + public boolean isVoiceMailNumber() { + return mIsVoiceMail; + } + + /** + * Mark this CallerInfo as an emergency call. + * @param context To lookup the localized 'Emergency Number' string. + * @return this instance. + */ + // TODO: Note we're setting the phone number here (refer to + // javadoc comments at the top of CallerInfo class) to a localized + // string 'Emergency Number'. This is pretty bad because we are + // making UI work here instead of just packaging the data. We + // should set the phone number to the dialed number and name to + // 'Emergency Number' and let the UI make the decision about what + // should be displayed. + /* package */ CallerInfo markAsEmergency(Context context) { + phoneNumber = context.getString( + com.android.internal.R.string.emergency_call_dialog_number_for_display); + photoResource = com.android.internal.R.drawable.picture_emergency; + mIsEmergency = true; + return this; + } + + + /* package */ CallerInfo markAsVoiceMail(Context context, int subId) { + mIsVoiceMail = true; + + try { + phoneNumber = context.getSystemService(TelephonyManager.class) + .createForSubscriptionId(subId) + .getVoiceMailAlphaTag(); + } catch (SecurityException se) { + // Should never happen: if this process does not have + // permission to retrieve VM tag, it should not have + // permission to retrieve VM number and would not call + // this method. + // Leave phoneNumber untouched. + Log.e(TAG, se, "Cannot access VoiceMail."); + } + // TODO: There is no voicemail picture? + // FIXME: FIND ANOTHER ICON + // photoResource = android.R.drawable.badge_voicemail; + return this; + } + + private static String normalize(String s) { + if (s == null || s.length() > 0) { + return s; + } else { + return null; + } + } + + /** + * Returns the column index to use to find the "person_id" field in + * the specified cursor, based on the contact URI that was originally + * queried. + * + * This is a helper function for the getCallerInfo() method that takes + * a Cursor. Looking up the person_id is nontrivial (compared to all + * the other CallerInfo fields) since the column we need to use + * depends on what query we originally ran. + * + * Watch out: be sure to not do any database access in this method, since + * it's run from the UI thread (see comments below for more info.) + * + * @return the columnIndex to use (with cursor.getLong()) to get the + * person_id, or -1 if we couldn't figure out what colum to use. + * + * TODO: Add a unittest for this method. (This is a little tricky to + * test, since we'll need a live contacts database to test against, + * preloaded with at least some phone numbers and SIP addresses. And + * we'll probably have to hardcode the column indexes we expect, so + * the test might break whenever the contacts schema changes. But we + * can at least make sure we handle all the URI patterns we claim to, + * and that the mime types match what we expect...) + */ + private static int getColumnIndexForPersonId(Uri contactRef, Cursor cursor) { + // TODO: This is pretty ugly now, see bug 2269240 for + // more details. The column to use depends upon the type of URL: + // - content://com.android.contacts/data/phones ==> use the "contact_id" column + // - content://com.android.contacts/phone_lookup ==> use the "_ID" column + // - content://com.android.contacts/data ==> use the "contact_id" column + // If it's none of the above, we leave columnIndex=-1 which means + // that the person_id field will be left unset. + // + // The logic here *used* to be based on the mime type of contactRef + // (for example Phone.CONTENT_ITEM_TYPE would tell us to use the + // RawContacts.CONTACT_ID column). But looking up the mime type requires + // a call to context.getContentResolver().getType(contactRef), which + // isn't safe to do from the UI thread since it can cause an ANR if + // the contacts provider is slow or blocked (like during a sync.) + // + // So instead, figure out the column to use for person_id by just + // looking at the URI itself. + + if (VDBG) Log.v(TAG, "- getColumnIndexForPersonId: contactRef URI = '" + + contactRef + "'..."); + // Warning: Do not enable the following logging (due to ANR risk.) + // if (VDBG) Log.v(TAG, "- MIME type: " + // + context.getContentResolver().getType(contactRef)); + + String url = contactRef.toString(); + String columnName = null; + if (url.startsWith("content://com.android.contacts/data/phones")) { + // Direct lookup in the Phone table. + // MIME type: Phone.CONTENT_ITEM_TYPE (= "vnd.android.cursor.item/phone_v2") + if (VDBG) Log.v(TAG, "'data/phones' URI; using RawContacts.CONTACT_ID"); + columnName = RawContacts.CONTACT_ID; + } else if (url.startsWith("content://com.android.contacts/data")) { + // Direct lookup in the Data table. + // MIME type: Data.CONTENT_TYPE (= "vnd.android.cursor.dir/data") + if (VDBG) Log.v(TAG, "'data' URI; using Data.CONTACT_ID"); + // (Note Data.CONTACT_ID and RawContacts.CONTACT_ID are equivalent.) + columnName = Data.CONTACT_ID; + } else if (url.startsWith("content://com.android.contacts/phone_lookup")) { + // Lookup in the PhoneLookup table, which provides "fuzzy matching" + // for phone numbers. + // MIME type: PhoneLookup.CONTENT_TYPE (= "vnd.android.cursor.dir/phone_lookup") + if (VDBG) Log.v(TAG, "'phone_lookup' URI; using PhoneLookup._ID"); + columnName = PhoneLookup._ID; + } else { + Log.w(TAG, "Unexpected prefix for contactRef '" + url + "'"); + } + int columnIndex = (columnName != null) ? cursor.getColumnIndex(columnName) : -1; + if (VDBG) Log.v(TAG, "==> Using column '" + columnName + + "' (columnIndex = " + columnIndex + ") for person_id lookup..."); + return columnIndex; + } + + /** + * Updates this CallerInfo's geoDescription field, based on the raw + * phone number in the phoneNumber field. + * + * (Note that the various getCallerInfo() methods do *not* set the + * geoDescription automatically; you need to call this method + * explicitly to get it.) + * + * @param context the context used to look up the current locale / country + * @param fallbackNumber if this CallerInfo's phoneNumber field is empty, + * this specifies a fallback number to use instead. + * @hide + */ + public void updateGeoDescription(Context context, String fallbackNumber) { + String number = TextUtils.isEmpty(phoneNumber) ? fallbackNumber : phoneNumber; + geoDescription = getGeoDescription(context, number); + } + + /** + * @return a geographical description string for the specified number. + * @see com.android.i18n.phonenumbers.PhoneNumberOfflineGeocoder + * + * @hide + */ + public static String getGeoDescription(Context context, String number) { + if (VDBG) Log.v(TAG, "getGeoDescription('" + number + "')..."); + + if (TextUtils.isEmpty(number)) { + return null; + } + + PhoneNumberUtil util = PhoneNumberUtil.getInstance(); + PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance(); + + Locale locale = context.getResources().getConfiguration().locale; + String countryIso = getCurrentCountryIso(context, locale); + PhoneNumber pn = null; + try { + if (VDBG) Log.v(TAG, "parsing '" + number + + "' for countryIso '" + countryIso + "'..."); + pn = util.parse(number, countryIso); + if (VDBG) Log.v(TAG, "- parsed number: " + pn); + } catch (NumberParseException e) { + Log.w(TAG, "getGeoDescription: NumberParseException for incoming number '" + + Log.pii(number) + "'"); + } + + if (pn != null) { + String description = geocoder.getDescriptionForNumber(pn, locale); + if (VDBG) Log.v(TAG, "- got description: '" + description + "'"); + return description; + } else { + return null; + } + } + + /** + * @return The ISO 3166-1 two letters country code of the country the user + * is in. + */ + private static String getCurrentCountryIso(Context context, Locale locale) { + String countryIso = null; + CountryDetector detector = (CountryDetector) context.getSystemService( + Context.COUNTRY_DETECTOR); + if (detector != null) { + Country country = detector.detectCountry(); + if (country != null) { + countryIso = country.getCountryIso(); + } else { + Log.e(TAG, new Exception(), "CountryDetector.detectCountry() returned null."); + } + } + if (countryIso == null) { + countryIso = locale.getCountry(); + Log.w(TAG, "No CountryDetector; falling back to countryIso based on locale: " + + countryIso); + } + return countryIso; + } + + /** @hide */ + protected static String getCurrentCountryIso(Context context) { + return getCurrentCountryIso(context, Locale.getDefault()); + } + + /** + * @return a string debug representation of this instance. + */ + @Override + public String toString() { + // Warning: never check in this file with VERBOSE_DEBUG = true + // because that will result in PII in the system log. + final boolean VERBOSE_DEBUG = false; + + if (VERBOSE_DEBUG) { + return new StringBuilder(384) + .append(super.toString() + " { ") + .append("\nname: " + name) + .append("\nphoneNumber: " + phoneNumber) + .append("\nnormalizedNumber: " + normalizedNumber) + .append("\ngeoDescription: " + geoDescription) + .append("\ncnapName: " + cnapName) + .append("\nnumberPresentation: " + numberPresentation) + .append("\nnamePresentation: " + namePresentation) + .append("\ncontactExits: " + contactExists) + .append("\nphoneLabel: " + phoneLabel) + .append("\nnumberType: " + numberType) + .append("\nnumberLabel: " + numberLabel) + .append("\nphotoResource: " + photoResource) + .append("\ncontactIdOrZero: " + contactIdOrZero) + .append("\nneedUpdate: " + needUpdate) + .append("\ncontactRingtoneUri: " + contactRingtoneUri) + .append("\ncontactDisplayPhotoUri: " + contactDisplayPhotoUri) + .append("\nshouldSendToVoicemail: " + shouldSendToVoicemail) + .append("\ncachedPhoto: " + cachedPhoto) + .append("\nisCachedPhotoCurrent: " + isCachedPhotoCurrent) + .append("\nemergency: " + mIsEmergency) + .append("\nvoicemail " + mIsVoiceMail) + .append("\ncontactExists " + contactExists) + .append("\nuserType " + userType) + .append(" }") + .toString(); + } else { + return new StringBuilder(128) + .append(super.toString() + " { ") + .append("name " + ((name == null) ? "null" : "non-null")) + .append(", phoneNumber " + ((phoneNumber == null) ? "null" : "non-null")) + .append(" }") + .toString(); + } + } +} diff --git a/telecomm/java/android/telecom/CallerInfoAsyncQuery.java b/telecomm/java/android/telecom/CallerInfoAsyncQuery.java new file mode 100644 index 000000000000..f38b34e85d89 --- /dev/null +++ b/telecomm/java/android/telecom/CallerInfoAsyncQuery.java @@ -0,0 +1,548 @@ +/* + * Copyright (C) 2006 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 android.telecom; + +import android.app.ActivityManager; +import android.content.AsyncQueryHandler; +import android.content.ContentResolver; +import android.content.Context; +import android.content.pm.PackageManager.NameNotFoundException; +import android.database.Cursor; +import android.database.SQLException; +import android.net.Uri; +import android.os.Handler; +import android.os.Looper; +import android.os.Message; +import android.os.SystemClock; +import android.os.UserHandle; +import android.os.UserManager; +import android.provider.ContactsContract.PhoneLookup; +import android.telephony.PhoneNumberUtils; +import android.telephony.SubscriptionManager; +import android.text.TextUtils; +import java.util.ArrayList; +import java.util.List; + +/** + * Helper class to make it easier to run asynchronous caller-id lookup queries. + * @see CallerInfo + * + * {@hide} + */ +public class CallerInfoAsyncQuery { + private static final boolean DBG = false; + private static final String LOG_TAG = "CallerInfoAsyncQuery"; + + private static final int EVENT_NEW_QUERY = 1; + private static final int EVENT_ADD_LISTENER = 2; + private static final int EVENT_END_OF_QUEUE = 3; + private static final int EVENT_EMERGENCY_NUMBER = 4; + private static final int EVENT_VOICEMAIL_NUMBER = 5; + private static final int EVENT_GET_GEO_DESCRIPTION = 6; + + private CallerInfoAsyncQueryHandler mHandler; + + // If the CallerInfo query finds no contacts, should we use the + // PhoneNumberOfflineGeocoder to look up a "geo description"? + // (TODO: This could become a flag in config.xml if it ever needs to be + // configured on a per-product basis.) + private static final boolean ENABLE_UNKNOWN_NUMBER_GEO_DESCRIPTION = true; + + /** + * Interface for a CallerInfoAsyncQueryHandler result return. + */ + public interface OnQueryCompleteListener { + /** + * Called when the query is complete. + */ + public void onQueryComplete(int token, Object cookie, CallerInfo ci); + } + + + /** + * Wrap the cookie from the WorkerArgs with additional information needed by our + * classes. + */ + private static final class CookieWrapper { + public OnQueryCompleteListener listener; + public Object cookie; + public int event; + public String number; + public String geoDescription; + + public int subId; + } + + + /** + * Simple exception used to communicate problems with the query pool. + */ + public static class QueryPoolException extends SQLException { + public QueryPoolException(String error) { + super(error); + } + } + + /** + * @return {@link ContentResolver} for the "current" user. + */ + static ContentResolver getCurrentProfileContentResolver(Context context) { + + if (DBG) Log.d(LOG_TAG, "Trying to get current content resolver..."); + + final int currentUser = ActivityManager.getCurrentUser(); + final int myUser = UserManager.get(context).getUserHandle(); + + if (DBG) Log.d(LOG_TAG, "myUser=" + myUser + "currentUser=" + currentUser); + + if (myUser != currentUser) { + final Context otherContext; + try { + otherContext = context.createPackageContextAsUser(context.getPackageName(), + /* flags =*/ 0, UserHandle.of(currentUser)); + return otherContext.getContentResolver(); + } catch (NameNotFoundException e) { + Log.e(LOG_TAG, e, "Can't find self package"); + // Fall back to the primary user. + } + } + return context.getContentResolver(); + } + + /** + * Our own implementation of the AsyncQueryHandler. + */ + private class CallerInfoAsyncQueryHandler extends AsyncQueryHandler { + + /* + * The information relevant to each CallerInfo query. Each query may have multiple + * listeners, so each AsyncCursorInfo is associated with 2 or more CookieWrapper + * objects in the queue (one with a new query event, and one with a end event, with + * 0 or more additional listeners in between). + */ + + /** + * Context passed by the caller. + * + * NOTE: The actual context we use for query may *not* be this context; since we query + * against the "current" contacts provider. In the constructor we pass the "current" + * context resolver (obtained via {@link #getCurrentProfileContentResolver) and pass it + * to the super class. + */ + private Context mContext; + private Uri mQueryUri; + private CallerInfo mCallerInfo; + private List<Runnable> mPendingListenerCallbacks = new ArrayList<>(); + + /** + * Our own query worker thread. + * + * This thread handles the messages enqueued in the looper. The normal sequence + * of events is that a new query shows up in the looper queue, followed by 0 or + * more add listener requests, and then an end request. Of course, these requests + * can be interlaced with requests from other tokens, but is irrelevant to this + * handler since the handler has no state. + * + * Note that we depend on the queue to keep things in order; in other words, the + * looper queue must be FIFO with respect to input from the synchronous startQuery + * calls and output to this handleMessage call. + * + * This use of the queue is required because CallerInfo objects may be accessed + * multiple times before the query is complete. All accesses (listeners) must be + * queued up and informed in order when the query is complete. + */ + protected class CallerInfoWorkerHandler extends WorkerHandler { + public CallerInfoWorkerHandler(Looper looper) { + super(looper); + } + + @Override + public void handleMessage(Message msg) { + WorkerArgs args = (WorkerArgs) msg.obj; + CookieWrapper cw = (CookieWrapper) args.cookie; + + if (cw == null) { + // Normally, this should never be the case for calls originating + // from within this code. + // However, if there is any code that this Handler calls (such as in + // super.handleMessage) that DOES place unexpected messages on the + // queue, then we need pass these messages on. + Log.i(LOG_TAG, "Unexpected command (CookieWrapper is null): " + msg.what + + " ignored by CallerInfoWorkerHandler, passing onto parent."); + + super.handleMessage(msg); + } else { + + Log.d(LOG_TAG, "Processing event: " + cw.event + " token (arg1): " + msg.arg1 + + " command: " + msg.what + " query URI: " + sanitizeUriToString(args.uri)); + + switch (cw.event) { + case EVENT_NEW_QUERY: + //start the sql command. + super.handleMessage(msg); + break; + + // shortcuts to avoid query for recognized numbers. + case EVENT_EMERGENCY_NUMBER: + case EVENT_VOICEMAIL_NUMBER: + + case EVENT_ADD_LISTENER: + case EVENT_END_OF_QUEUE: + // query was already completed, so just send the reply. + // passing the original token value back to the caller + // on top of the event values in arg1. + Message reply = args.handler.obtainMessage(msg.what); + reply.obj = args; + reply.arg1 = msg.arg1; + + reply.sendToTarget(); + + break; + case EVENT_GET_GEO_DESCRIPTION: + handleGeoDescription(msg); + break; + default: + } + } + } + + private void handleGeoDescription(Message msg) { + WorkerArgs args = (WorkerArgs) msg.obj; + CookieWrapper cw = (CookieWrapper) args.cookie; + if (!TextUtils.isEmpty(cw.number) && cw.cookie != null && mContext != null) { + final long startTimeMillis = SystemClock.elapsedRealtime(); + cw.geoDescription = CallerInfo.getGeoDescription(mContext, cw.number); + final long duration = SystemClock.elapsedRealtime() - startTimeMillis; + if (duration > 500) { + if (DBG) Log.d(LOG_TAG, "[handleGeoDescription]" + + "Spends long time to retrieve Geo description: " + duration); + } + } + Message reply = args.handler.obtainMessage(msg.what); + reply.obj = args; + reply.arg1 = msg.arg1; + reply.sendToTarget(); + } + } + + + /** + * Asynchronous query handler class for the contact / callerinfo object. + */ + private CallerInfoAsyncQueryHandler(Context context) { + super(getCurrentProfileContentResolver(context)); + mContext = context; + } + + @Override + protected Handler createHandler(Looper looper) { + return new CallerInfoWorkerHandler(looper); + } + + /** + * Overrides onQueryComplete from AsyncQueryHandler. + * + * This method takes into account the state of this class; we construct the CallerInfo + * object only once for each set of listeners. When the query thread has done its work + * and calls this method, we inform the remaining listeners in the queue, until we're + * out of listeners. Once we get the message indicating that we should expect no new + * listeners for this CallerInfo object, we release the AsyncCursorInfo back into the + * pool. + */ + @Override + protected void onQueryComplete(int token, Object cookie, Cursor cursor) { + Log.d(LOG_TAG, "##### onQueryComplete() ##### query complete for token: " + token); + + //get the cookie and notify the listener. + CookieWrapper cw = (CookieWrapper) cookie; + if (cw == null) { + // Normally, this should never be the case for calls originating + // from within this code. + // However, if there is any code that calls this method, we should + // check the parameters to make sure they're viable. + Log.i(LOG_TAG, "Cookie is null, ignoring onQueryComplete() request."); + if (cursor != null) { + cursor.close(); + } + return; + } + + if (cw.event == EVENT_END_OF_QUEUE) { + for (Runnable r : mPendingListenerCallbacks) { + r.run(); + } + mPendingListenerCallbacks.clear(); + + release(); + if (cursor != null) { + cursor.close(); + } + return; + } + + // If the cw.event == EVENT_GET_GEO_DESCRIPTION, means it would not be the 1st + // time entering the onQueryComplete(), mCallerInfo should not be null. + if (cw.event == EVENT_GET_GEO_DESCRIPTION) { + if (mCallerInfo != null) { + mCallerInfo.geoDescription = cw.geoDescription; + } + // notify that we can clean up the queue after this. + CookieWrapper endMarker = new CookieWrapper(); + endMarker.event = EVENT_END_OF_QUEUE; + startQuery(token, endMarker, null, null, null, null, null); + } + + // check the token and if needed, create the callerinfo object. + if (mCallerInfo == null) { + if ((mContext == null) || (mQueryUri == null)) { + throw new QueryPoolException + ("Bad context or query uri, or CallerInfoAsyncQuery already released."); + } + + // adjust the callerInfo data as needed, and only if it was set from the + // initial query request. + // Change the callerInfo number ONLY if it is an emergency number or the + // voicemail number, and adjust other data (including photoResource) + // accordingly. + if (cw.event == EVENT_EMERGENCY_NUMBER) { + // Note we're setting the phone number here (refer to javadoc + // comments at the top of CallerInfo class). + mCallerInfo = new CallerInfo().markAsEmergency(mContext); + } else if (cw.event == EVENT_VOICEMAIL_NUMBER) { + mCallerInfo = new CallerInfo().markAsVoiceMail(mContext, cw.subId); + } else { + mCallerInfo = CallerInfo.getCallerInfo(mContext, mQueryUri, cursor); + if (DBG) Log.d(LOG_TAG, "==> Got mCallerInfo: " + mCallerInfo); + + CallerInfo newCallerInfo = CallerInfo.doSecondaryLookupIfNecessary( + mContext, cw.number, mCallerInfo); + if (newCallerInfo != mCallerInfo) { + mCallerInfo = newCallerInfo; + if (DBG) Log.d(LOG_TAG, "#####async contact look up with numeric username" + + mCallerInfo); + } + + // Use the number entered by the user for display. + if (!TextUtils.isEmpty(cw.number)) { + mCallerInfo.setPhoneNumber(PhoneNumberUtils.formatNumber(cw.number, + mCallerInfo.normalizedNumber, + CallerInfo.getCurrentCountryIso(mContext))); + } + + // This condition refer to the google default code for geo. + // If the number exists in Contacts, the CallCard would never show + // the geo description, so it would be unnecessary to query it. + if (ENABLE_UNKNOWN_NUMBER_GEO_DESCRIPTION) { + if (TextUtils.isEmpty(mCallerInfo.getName())) { + if (DBG) Log.d(LOG_TAG, "start querying geo description"); + cw.event = EVENT_GET_GEO_DESCRIPTION; + startQuery(token, cw, null, null, null, null, null); + return; + } + } + } + + if (DBG) Log.d(LOG_TAG, "constructing CallerInfo object for token: " + token); + + //notify that we can clean up the queue after this. + CookieWrapper endMarker = new CookieWrapper(); + endMarker.event = EVENT_END_OF_QUEUE; + startQuery(token, endMarker, null, null, null, null, null); + } + + //notify the listener that the query is complete. + if (cw.listener != null) { + mPendingListenerCallbacks.add(new Runnable() { + @Override + public void run() { + if (DBG) Log.d(LOG_TAG, "notifying listener: " + + cw.listener.getClass().toString() + " for token: " + token + + mCallerInfo); + cw.listener.onQueryComplete(token, cw.cookie, mCallerInfo); + } + }); + } else { + Log.w(LOG_TAG, "There is no listener to notify for this query."); + } + + if (cursor != null) { + cursor.close(); + } + } + } + + /** + * Private constructor for factory methods. + */ + private CallerInfoAsyncQuery() { + } + + + /** + * Factory method to start query with a Uri query spec + */ + public static CallerInfoAsyncQuery startQuery(int token, Context context, Uri contactRef, + OnQueryCompleteListener listener, Object cookie) { + + CallerInfoAsyncQuery c = new CallerInfoAsyncQuery(); + c.allocate(context, contactRef); + + if (DBG) Log.d(LOG_TAG, "starting query for URI: " + contactRef + " handler: " + c.toString()); + + //create cookieWrapper, start query + CookieWrapper cw = new CookieWrapper(); + cw.listener = listener; + cw.cookie = cookie; + cw.event = EVENT_NEW_QUERY; + + c.mHandler.startQuery(token, cw, contactRef, null, null, null, null); + + return c; + } + + /** + * Factory method to start the query based on a number. + * + * Note: if the number contains an "@" character we treat it + * as a SIP address, and look it up directly in the Data table + * rather than using the PhoneLookup table. + * TODO: But eventually we should expose two separate methods, one for + * numbers and one for SIP addresses, and then have + * PhoneUtils.startGetCallerInfo() decide which one to call based on + * the phone type of the incoming connection. + */ + public static CallerInfoAsyncQuery startQuery(int token, Context context, String number, + OnQueryCompleteListener listener, Object cookie) { + + int subId = SubscriptionManager.getDefaultSubscriptionId(); + return startQuery(token, context, number, listener, cookie, subId); + } + + /** + * Factory method to start the query based on a number with specific subscription. + * + * Note: if the number contains an "@" character we treat it + * as a SIP address, and look it up directly in the Data table + * rather than using the PhoneLookup table. + * TODO: But eventually we should expose two separate methods, one for + * numbers and one for SIP addresses, and then have + * PhoneUtils.startGetCallerInfo() decide which one to call based on + * the phone type of the incoming connection. + */ + public static CallerInfoAsyncQuery startQuery(int token, Context context, String number, + OnQueryCompleteListener listener, Object cookie, int subId) { + + if (DBG) { + Log.d(LOG_TAG, "##### CallerInfoAsyncQuery startQuery()... #####"); + Log.d(LOG_TAG, "- number: " + /*number*/ "xxxxxxx"); + Log.d(LOG_TAG, "- cookie: " + cookie); + } + + // Construct the URI object and query params, and start the query. + + final Uri contactRef = PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI.buildUpon() + .appendPath(number) + .appendQueryParameter(PhoneLookup.QUERY_PARAMETER_SIP_ADDRESS, + String.valueOf(PhoneNumberUtils.isUriNumber(number))) + .build(); + + if (DBG) { + Log.d(LOG_TAG, "==> contactRef: " + sanitizeUriToString(contactRef)); + } + + CallerInfoAsyncQuery c = new CallerInfoAsyncQuery(); + c.allocate(context, contactRef); + + //create cookieWrapper, start query + CookieWrapper cw = new CookieWrapper(); + cw.listener = listener; + cw.cookie = cookie; + cw.number = number; + cw.subId = subId; + + // check to see if these are recognized numbers, and use shortcuts if we can. + if (PhoneNumberUtils.isLocalEmergencyNumber(context, number)) { + cw.event = EVENT_EMERGENCY_NUMBER; + } else if (PhoneNumberUtils.isVoiceMailNumber(context, subId, number)) { + cw.event = EVENT_VOICEMAIL_NUMBER; + } else { + cw.event = EVENT_NEW_QUERY; + } + + c.mHandler.startQuery(token, + cw, // cookie + contactRef, // uri + null, // projection + null, // selection + null, // selectionArgs + null); // orderBy + return c; + } + + /** + * Method to add listeners to a currently running query + */ + public void addQueryListener(int token, OnQueryCompleteListener listener, Object cookie) { + + if (DBG) Log.d(LOG_TAG, "adding listener to query: " + + sanitizeUriToString(mHandler.mQueryUri) + " handler: " + mHandler.toString()); + + //create cookieWrapper, add query request to end of queue. + CookieWrapper cw = new CookieWrapper(); + cw.listener = listener; + cw.cookie = cookie; + cw.event = EVENT_ADD_LISTENER; + + mHandler.startQuery(token, cw, null, null, null, null, null); + } + + /** + * Method to create a new CallerInfoAsyncQueryHandler object, ensuring correct + * state of context and uri. + */ + private void allocate(Context context, Uri contactRef) { + if ((context == null) || (contactRef == null)){ + throw new QueryPoolException("Bad context or query uri."); + } + mHandler = new CallerInfoAsyncQueryHandler(context); + mHandler.mQueryUri = contactRef; + } + + /** + * Releases the relevant data. + */ + private void release() { + mHandler.mContext = null; + mHandler.mQueryUri = null; + mHandler.mCallerInfo = null; + mHandler = null; + } + + private static String sanitizeUriToString(Uri uri) { + if (uri != null) { + String uriString = uri.toString(); + int indexOfLastSlash = uriString.lastIndexOf('/'); + if (indexOfLastSlash > 0) { + return uriString.substring(0, indexOfLastSlash) + "/xxxxxxx"; + } else { + return uriString; + } + } else { + return ""; + } + } +} diff --git a/telecomm/java/android/telecom/Connection.java b/telecomm/java/android/telecom/Connection.java index 2ce84fbc0dc7..4f373af984fa 100644 --- a/telecomm/java/android/telecom/Connection.java +++ b/telecomm/java/android/telecom/Connection.java @@ -857,7 +857,7 @@ public abstract class Connection extends Conferenceable { } /** @hide */ - public abstract static class Listener { + abstract static class Listener { public void onStateChanged(Connection c, int state) {} public void onAddressChanged(Connection c, Uri newAddress, int presentation) {} public void onCallerDisplayNameChanged( @@ -2005,7 +2005,7 @@ public abstract class Connection extends Conferenceable { * * @hide */ - public final Connection addConnectionListener(Listener l) { + final Connection addConnectionListener(Listener l) { mListeners.add(l); return this; } @@ -2018,7 +2018,7 @@ public abstract class Connection extends Conferenceable { * * @hide */ - public final Connection removeConnectionListener(Listener l) { + final Connection removeConnectionListener(Listener l) { if (l != null) { mListeners.remove(l); } diff --git a/telecomm/java/android/telecom/TelecomManager.java b/telecomm/java/android/telecom/TelecomManager.java index 12066c4caeb6..2bc20d51113a 100644 --- a/telecomm/java/android/telecom/TelecomManager.java +++ b/telecomm/java/android/telecom/TelecomManager.java @@ -33,6 +33,7 @@ import android.os.Bundle; import android.os.RemoteException; import android.os.ServiceManager; import android.os.UserHandle; +import android.telephony.Annotation.CallState; import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; import android.text.TextUtils; @@ -1412,7 +1413,7 @@ public class TelecomManager { * @hide */ @SystemApi - public @TelephonyManager.CallState int getCallState() { + public @CallState int getCallState() { try { if (isServiceConnected()) { return getTelecomService().getCallState(); |