Converting CallerInfo to new Contacts API.

This commit is contained in:
Dmitri Plotnikov
2009-08-19 15:56:30 -07:00
parent d2e10d5912
commit 3c513ed95c
4 changed files with 132 additions and 84 deletions

View File

@@ -27,8 +27,7 @@ import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.os.Looper; import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.provider.Contacts; import android.provider.ContactsContract.Contacts;
import android.provider.Contacts.People;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
@@ -148,7 +147,7 @@ public class ContactsAsyncHelper extends Handler {
*/ */
public Uri getPhotoUri() { public Uri getPhotoUri() {
if (mCurrentCallerInfo != null) { if (mCurrentCallerInfo != null) {
return ContentUris.withAppendedId(People.CONTENT_URI, return ContentUris.withAppendedId(Contacts.CONTENT_URI,
mCurrentCallerInfo.person_id); mCurrentCallerInfo.person_id);
} }
return null; return null;
@@ -178,12 +177,13 @@ public class ContactsAsyncHelper extends Handler {
super(looper); super(looper);
} }
@Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
WorkerArgs args = (WorkerArgs) msg.obj; WorkerArgs args = (WorkerArgs) msg.obj;
switch (msg.arg1) { switch (msg.arg1) {
case EVENT_LOAD_IMAGE: case EVENT_LOAD_IMAGE:
InputStream inputStream = Contacts.People.openContactPhotoInputStream( InputStream inputStream = Contacts.openContactPhotoInputStream(
args.context.getContentResolver(), args.uri); args.context.getContentResolver(), args.uri);
if (inputStream != null) { if (inputStream != null) {
args.result = Drawable.createFromStream(inputStream, args.uri.toString()); args.result = Drawable.createFromStream(inputStream, args.uri.toString());

View File

@@ -19,15 +19,21 @@ package android.provider;
import android.accounts.Account; import android.accounts.Account;
import android.content.ContentProviderClient; import android.content.ContentProviderClient;
import android.content.ContentProviderOperation; import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.os.RemoteException; import android.os.RemoteException;
import android.provider.ContactsContract.CommonDataKinds.GroupMembership; import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
import android.text.TextUtils; import android.text.TextUtils;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
/** /**
* The contract between the contacts provider and applications. Contains definitions * The contract between the contacts provider and applications. Contains definitions
* for the supported URIs and columns. * for the supported URIs and columns.
@@ -299,6 +305,55 @@ public final class ContactsContract {
*/ */
public static final String CONTENT_DIRECTORY = "suggestions"; public static final String CONTENT_DIRECTORY = "suggestions";
} }
/**
* Returns a URI that can be used to retrieve the contact's default photo.
*
* @param contactUri the contact whose photo should be used
*/
public static Uri getPhotoUri(ContentResolver cr, Uri contactUri) {
long photoId = -1;
Cursor cursor = cr.query(contactUri, new String[]{Contacts.PHOTO_ID}, null, null, null);
try {
if (!cursor.moveToNext()) {
return null;
}
if (cursor.isNull(0)) {
return null;
}
photoId = cursor.getLong(0);
} finally {
cursor.close();
}
return ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, photoId);
}
/**
* Opens an InputStream for the person's default photo and returns the
* photo as a Bitmap stream.
*
* @param contactUri the contact whose photo should be used
*/
public static InputStream openContactPhotoInputStream(ContentResolver cr, Uri contactUri) {
Uri photoUri = getPhotoUri(cr, contactUri);
Cursor cursor = cr.query(photoUri,
new String[]{ContactsContract.CommonDataKinds.Photo.PHOTO}, null, null, null);
try {
if (!cursor.moveToNext()) {
return null;
}
byte[] data = cursor.getBlob(0);
if (data == null) {
return null;
}
return new ByteArrayInputStream(data);
} finally {
cursor.close();
}
}
} }
private interface RawContactsColumns { private interface RawContactsColumns {

View File

@@ -20,9 +20,8 @@ import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.provider.Contacts; import android.provider.ContactsContract.PhoneLookup;
import android.provider.Contacts.People; import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.Contacts.Phones;
import android.text.TextUtils; import android.text.TextUtils;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.telephony.PhoneNumberUtils; import android.telephony.PhoneNumberUtils;
@@ -134,44 +133,39 @@ public class CallerInfo {
int columnIndex; int columnIndex;
// Look for the name // Look for the name
columnIndex = cursor.getColumnIndex(People.NAME); columnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
if (columnIndex != -1) { if (columnIndex != -1) {
info.name = cursor.getString(columnIndex); info.name = cursor.getString(columnIndex);
} }
// Look for the number // Look for the number
columnIndex = cursor.getColumnIndex(Phones.NUMBER); columnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
if (columnIndex != -1) { if (columnIndex != -1) {
info.phoneNumber = cursor.getString(columnIndex); info.phoneNumber = cursor.getString(columnIndex);
} }
// Look for the label/type combo // Look for the label/type combo
columnIndex = cursor.getColumnIndex(Phones.LABEL); columnIndex = cursor.getColumnIndex(PhoneLookup.LABEL);
if (columnIndex != -1) { if (columnIndex != -1) {
int typeColumnIndex = cursor.getColumnIndex(Phones.TYPE); int typeColumnIndex = cursor.getColumnIndex(PhoneLookup.TYPE);
if (typeColumnIndex != -1) { if (typeColumnIndex != -1) {
info.numberType = cursor.getInt(typeColumnIndex); info.numberType = cursor.getInt(typeColumnIndex);
info.numberLabel = cursor.getString(columnIndex); info.numberLabel = cursor.getString(columnIndex);
info.phoneLabel = Contacts.Phones.getDisplayLabel(context, info.phoneLabel = Phone.getDisplayLabel(context,
info.numberType, info.numberLabel) info.numberType, info.numberLabel)
.toString(); .toString();
} }
} }
// Look for the person ID // Look for the person ID
columnIndex = cursor.getColumnIndex(Phones.PERSON_ID); columnIndex = cursor.getColumnIndex(PhoneLookup._ID);
if (columnIndex != -1) { if (columnIndex != -1) {
info.person_id = cursor.getLong(columnIndex); info.person_id = cursor.getLong(columnIndex);
} else {
columnIndex = cursor.getColumnIndex(People._ID);
if (columnIndex != -1) {
info.person_id = cursor.getLong(columnIndex);
}
} }
// look for the custom ringtone, create from the string stored // look for the custom ringtone, create from the string stored
// in the database. // in the database.
columnIndex = cursor.getColumnIndex(People.CUSTOM_RINGTONE); columnIndex = cursor.getColumnIndex(PhoneLookup.CUSTOM_RINGTONE);
if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) { if ((columnIndex != -1) && (cursor.getString(columnIndex) != null)) {
info.contactRingtoneUri = Uri.parse(cursor.getString(columnIndex)); info.contactRingtoneUri = Uri.parse(cursor.getString(columnIndex));
} else { } else {
@@ -180,7 +174,7 @@ public class CallerInfo {
// look for the send to voicemail flag, set it to true only // look for the send to voicemail flag, set it to true only
// under certain circumstances. // under certain circumstances.
columnIndex = cursor.getColumnIndex(People.SEND_TO_VOICEMAIL); columnIndex = cursor.getColumnIndex(PhoneLookup.SEND_TO_VOICEMAIL);
info.shouldSendToVoicemail = (columnIndex != -1) && info.shouldSendToVoicemail = (columnIndex != -1) &&
((cursor.getInt(columnIndex)) == 1); ((cursor.getInt(columnIndex)) == 1);
info.contactExists = true; info.contactExists = true;
@@ -256,8 +250,7 @@ public class CallerInfo {
} }
} }
Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, number);
Uri.encode(number));
CallerInfo info = getCallerInfo(context, contactUri); CallerInfo info = getCallerInfo(context, contactUri);

View File

@@ -24,7 +24,7 @@ import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.provider.Contacts; import android.provider.ContactsContract.PhoneLookup;
import android.telephony.PhoneNumberUtils; import android.telephony.PhoneNumberUtils;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.text.TextUtils; import android.text.TextUtils;
@@ -303,7 +303,7 @@ public class CallerInfoAsyncQuery {
public static CallerInfoAsyncQuery startQuery(int token, Context context, String number, public static CallerInfoAsyncQuery startQuery(int token, Context context, String number,
OnQueryCompleteListener listener, Object cookie) { OnQueryCompleteListener listener, Object cookie) {
//contruct the URI object and start Query. //contruct the URI object and start Query.
Uri contactRef = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, number); Uri contactRef = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, number);
CallerInfoAsyncQuery c = new CallerInfoAsyncQuery(); CallerInfoAsyncQuery c = new CallerInfoAsyncQuery();
c.allocate(context, contactRef); c.allocate(context, contactRef);