下面列出了怎么用android.provider.ContactsContract.CommonDataKinds的API类实例代码及写法,或者点击链接到github查看源代码。
public void setPhoto(byte[] photo) {
if (photo != null) {
if (isAndroidContact()) {
if (androidRawId != null) {
changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, androidRawId)
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.Photo.PHOTO, photo)
.withValue(ContactsContract.Data.IS_PRIMARY, 1)
.withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
.build());
} else {
changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.Photo.PHOTO, photo)
.build());
}
}
}
}
@Override
public Intent getAddContactIntent(String displayName, String csipUri) {
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT, Contacts.CONTENT_URI);
intent.setType(Contacts.CONTENT_ITEM_TYPE);
if (!TextUtils.isEmpty(displayName)) {
intent.putExtra(Insert.NAME, displayName);
}
if (!TextUtils.isEmpty(csipUri)) {
ArrayList<ContentValues> data = new ArrayList<ContentValues>();
ContentValues csipProto = new ContentValues();
csipProto.put(Data.MIMETYPE, CommonDataKinds.Im.CONTENT_ITEM_TYPE);
csipProto.put(CommonDataKinds.Im.PROTOCOL, CommonDataKinds.Im.PROTOCOL_CUSTOM);
csipProto.put(CommonDataKinds.Im.CUSTOM_PROTOCOL, SipManager.PROTOCOL_CSIP);
csipProto.put(CommonDataKinds.Im.DATA, SipUri.getCanonicalSipContact(csipUri, false));
data.add(csipProto);
intent.putParcelableArrayListExtra(Insert.DATA, data);
}
return intent;
}
private int mapStringToEmailType(String label) {
int emailType;
switch (label) {
case "home":
emailType = CommonDataKinds.Email.TYPE_HOME;
break;
case "work":
emailType = CommonDataKinds.Email.TYPE_WORK;
break;
case "mobile":
emailType = CommonDataKinds.Email.TYPE_MOBILE;
break;
default:
emailType = CommonDataKinds.Email.TYPE_CUSTOM;
break;
}
return emailType;
}
public static Photo getPhoto(ContentResolver c, long rawContactId){
Photo photo = new Photo();
String where = ContactsContract.Data.RAW_CONTACT_ID + " = '" + rawContactId
+ "' AND " + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
Cursor c1 = c.query(ContactsContract.Data.CONTENT_URI, new String[] {ContactsContract.CommonDataKinds.Photo.PHOTO, ContactsContract.Data.SYNC2, ContactsContract.Data.SYNC3 }, where , null, null);
if (c1.getCount() > 0){
c1.moveToLast();
photo.data = c1.getBlob(c1.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO));
photo.timestamp = Long.valueOf(c1.getString(c1.getColumnIndex(ContactsContract.Data.SYNC2)));
photo.url = c1.getString(c1.getColumnIndex(ContactsContract.Data.SYNC3));
}
c1.close();
return photo;
}
public static void addEmail(Context c, long rawContactId, String email){
DeviceUtil.log(c, "adding email", email);
String where = ContactsContract.Data.RAW_CONTACT_ID + " = '" + rawContactId
+ "' AND " + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE+ "'";
Cursor cursor = c.getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] { RawContacts.CONTACT_ID}, where, null, null);
if (cursor.getCount() == 0){
ContentValues contentValues = new ContentValues();
//op.put(ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID, );
contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
contentValues.put(ContactsContract.CommonDataKinds.Email.ADDRESS, email);
c.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, contentValues);
}
cursor.close();
}
public static void addBirthday(long rawContactId, String birthday){
String where = ContactsContract.Data.RAW_CONTACT_ID + " = '" + rawContactId
+ "' AND " + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE
+ "' AND " + ContactsContract.CommonDataKinds.Event.TYPE + " = '" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY + "'";
Cursor cursor = ContactsSyncAdapterService.mContentResolver.query(ContactsContract.Data.CONTENT_URI, null, where, null, null);
int count = cursor.getCount();
cursor.close();
if (count <= 0){
ContentValues contentValues = new ContentValues();
contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
contentValues.put(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY);
contentValues.put(ContactsContract.CommonDataKinds.Event.START_DATE, birthday);
try {
ContactsSyncAdapterService.mContentResolver.insert(ContactsContract.Data.CONTENT_URI, contentValues);
// mContentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
} catch (Exception e) {
e.printStackTrace();
//Log.e("ERROR:" , e.^);
}
}
}
private Cursor getContactsCursor(ContentResolver cr) {
String req = "(" + Data.MIMETYPE + " = '" + CommonDataKinds.Phone.CONTENT_ITEM_TYPE
+ "' AND " + CommonDataKinds.Phone.NUMBER + " IS NOT NULL "
+ " OR (" + Data.MIMETYPE + " = '" + CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE
+ "' AND " + CommonDataKinds.SipAddress.SIP_ADDRESS + " IS NOT NULL))";
String[] projection = new String[] { Data.CONTACT_ID, Data.DISPLAY_NAME };
String query = Data.DISPLAY_NAME + " IS NOT NULL AND (" + req + ")";
Cursor cursor = cr.query(Data.CONTENT_URI, projection, query, null, " lower(" + Data.DISPLAY_NAME + ") COLLATE UNICODE ASC");
if (cursor == null) {
return cursor;
}
MatrixCursor result = new MatrixCursor(cursor.getColumnNames());
Set<String> groupBy = new HashSet<String>();
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(Data.DISPLAY_NAME));
if (!groupBy.contains(name)) {
groupBy.add(name);
Object[] newRow = new Object[cursor.getColumnCount()];
int contactID = cursor.getColumnIndex(Data.CONTACT_ID);
int displayName = cursor.getColumnIndex(Data.DISPLAY_NAME);
newRow[contactID] = cursor.getString(contactID);
newRow[displayName] = cursor.getString(displayName);
result.addRow(newRow);
}
}
cursor.close();
return result;
}
public void setFirstNameAndLastName(String fn, String ln) {
if (fn != null && fn.length() == 0 && ln != null && ln.length() == 0) return;
if (isAndroidContact()) {
if (firstName != null || lastName != null) {
String select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'";
String[] args = new String[]{ getAndroidId() };
changesToCommit.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(select, args)
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.StructuredName.GIVEN_NAME, fn)
.withValue(CommonDataKinds.StructuredName.FAMILY_NAME, ln)
.build()
);
} else {
changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.StructuredName.GIVEN_NAME, fn)
.withValue(CommonDataKinds.StructuredName.FAMILY_NAME, ln)
.build());
}
}
firstName = fn;
lastName = ln;
if (firstName != null && lastName != null && firstName.length() > 0 && lastName.length() > 0) {
fullName = firstName + " " + lastName;
} else if (firstName != null && firstName.length() > 0) {
fullName = firstName;
} else if (lastName != null && lastName.length() > 0) {
fullName = lastName;
}
}
public void setOrganization(String org) {
if (isAndroidContact()) {
if (androidRawId != null) {
String select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + CommonDataKinds.Organization.CONTENT_ITEM_TYPE + "'";
String[] args = new String[]{ getAndroidId() };
if (organization != null) {
changesToCommit.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(select, args)
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.Organization.COMPANY, org)
.build());
} else {
changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, androidRawId)
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.Organization.COMPANY, org)
.build());
}
} else {
changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.Organization.COMPANY, org)
.build());
}
}
organization = org;
}
private void getContactNames() {
ContentResolver resolver = ContactsManager.getInstance().getContentResolver();
String[] proj = new String[]{ CommonDataKinds.StructuredName.GIVEN_NAME, CommonDataKinds.StructuredName.FAMILY_NAME, ContactsContract.Contacts.DISPLAY_NAME };
String select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?";
String[] args = new String[]{ getAndroidId(), CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
Cursor c = resolver.query(ContactsContract.Data.CONTENT_URI, proj, select, args, null);
if (c != null) {
if (c.moveToFirst()) {
firstName = c.getString(c.getColumnIndex(CommonDataKinds.StructuredName.GIVEN_NAME));
lastName = c.getString(c.getColumnIndex(CommonDataKinds.StructuredName.FAMILY_NAME));
fullName = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
c.close();
}
}
private void getNativeContactOrganization() {
ContentResolver resolver = ContactsManager.getInstance().getContentResolver();
String[] proj = new String[]{ CommonDataKinds.Organization.COMPANY };
String select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?";
String[] args = new String[]{ getAndroidId(), CommonDataKinds.Organization.CONTENT_ITEM_TYPE };
Cursor c = resolver.query(ContactsContract.Data.CONTENT_URI, proj, select, args, null);
if (c != null) {
if (c.moveToFirst()) {
organization = c.getString(c.getColumnIndex(CommonDataKinds.Organization.COMPANY));
}
c.close();
}
}
private List<LinphoneNumberOrAddress> getAddressesAndNumbersForAndroidContact() {
List<LinphoneNumberOrAddress> result = new ArrayList<LinphoneNumberOrAddress>();
ContentResolver resolver = ContactsManager.getInstance().getContentResolver();
String select = ContactsContract.Data.CONTACT_ID + " =? AND (" + ContactsContract.Data.MIMETYPE + "=? OR " + ContactsContract.Data.MIMETYPE + "=?)";
String[] projection = new String[] { CommonDataKinds.SipAddress.SIP_ADDRESS, ContactsContract.Data.MIMETYPE }; // PHONE_NUMBER == SIP_ADDRESS == "data1"...
Cursor c = resolver.query(ContactsContract.Data.CONTENT_URI, projection, select, new String[]{ getAndroidId(), CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE }, null);
if (c != null) {
while (c.moveToNext()) {
String mime = c.getString(c.getColumnIndex(ContactsContract.Data.MIMETYPE));
if (mime != null && mime.length() > 0) {
boolean found = false;
boolean isSIP = false;
if (mime.equals(CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE)) {
found = true;
isSIP = true;
} else if (mime.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
found = true;
}
if (found) {
String number = c.getString(c.getColumnIndex(CommonDataKinds.SipAddress.SIP_ADDRESS)); // PHONE_NUMBER == SIP_ADDRESS == "data1"...
if (number != null && number.length() > 0) {
if (isSIP && !number.startsWith("sip:")) {
number = "sip:" + number;
}
if (isSIP && !number.contains("@")) {
number = number + "@" + ContactsManager.getInstance().getString(R.string.default_domain);
}
result.add(new LinphoneNumberOrAddress(number, isSIP));
}
}
}
}
c.close();
}
Collections.sort(result);
return result;
}
private void createLinphoneContactTag() {
ArrayList<ContentProviderOperation> batch = new ArrayList<ContentProviderOperation>();
batch.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, ContactsManager.getInstance().getString(R.string.sync_account_type))
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, ContactsManager.getInstance().getString(R.string.sync_account_name))
.withValue(ContactsContract.RawContacts.AGGREGATION_MODE, ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT)
.build());
batch.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.StructuredName.DISPLAY_NAME, getFullName())
.build());
batch.add(ContentProviderOperation.newUpdate(ContactsContract.AggregationExceptions.CONTENT_URI)
.withValue(ContactsContract.AggregationExceptions.TYPE, ContactsContract.AggregationExceptions.TYPE_KEEP_TOGETHER)
.withValue(ContactsContract.AggregationExceptions.RAW_CONTACT_ID1, androidRawId)
.withValueBackReference(ContactsContract.AggregationExceptions.RAW_CONTACT_ID2, 0)
.build());
if (changesToCommit2.size() > 0) {
for(ContentProviderOperation cpo : changesToCommit2) {
batch.add(cpo);
}
}
try {
ContactsManager.getInstance().getContentResolver().applyBatch(ContactsContract.AUTHORITY, batch);
androidTagId = findLinphoneRawContactId();
} catch (Exception e) {
Log.e(e);
}
}
@Override
public List<String> getCSipPhonesContact(Context ctxt, Long contactId) {
ArrayList<String> results = new ArrayList<String>();
Uri dataUri = Data.CONTENT_URI;
String dataQuery = Data.MIMETYPE + "='" + CommonDataKinds.Im.CONTENT_ITEM_TYPE + "' "
+ " AND "
+ CommonDataKinds.Im.PROTOCOL + "=" + CommonDataKinds.Im.PROTOCOL_CUSTOM
+ " AND "
+ " LOWER(" + CommonDataKinds.Im.CUSTOM_PROTOCOL + ")='"+SipManager.PROTOCOL_CSIP+"'";
// get csip data
Cursor dataCursor = ctxt.getContentResolver()
.query(dataUri,
new String[] {
CommonDataKinds.Im._ID,
CommonDataKinds.Im.DATA,
},
dataQuery + " AND " + CommonDataKinds.Im.CONTACT_ID + "=?",
new String[] {
Long.toString(contactId)
}, null);
try {
if (dataCursor != null && dataCursor.getCount() > 0) {
dataCursor.moveToFirst();
String val = dataCursor.getString(dataCursor
.getColumnIndex(CommonDataKinds.Im.DATA));
if (!TextUtils.isEmpty(val)) {
results.add(val);
}
}
} catch (Exception e) {
Log.e(THIS_FILE, "Error while looping on data", e);
} finally {
dataCursor.close();
}
return results;
}
private String getLabelFromType(Integer type, String name) {
if (type == null) {
throw new InvalidCursorTypeException();
}
switch (type) {
case CommonDataKinds.Relation.TYPE_CUSTOM:
return name;
case CommonDataKinds.Relation.TYPE_ASSISTANT:
return "assistant";
case CommonDataKinds.Relation.TYPE_BROTHER:
return "brother";
case CommonDataKinds.Relation.TYPE_CHILD:
return "child";
case CommonDataKinds.Relation.TYPE_DOMESTIC_PARTNER:
return "domestic partner";
case CommonDataKinds.Relation.TYPE_FATHER:
return "father";
case CommonDataKinds.Relation.TYPE_FRIEND:
return "friend";
case CommonDataKinds.Relation.TYPE_MANAGER:
return "manager";
case CommonDataKinds.Relation.TYPE_MOTHER:
return "mother";
case CommonDataKinds.Relation.TYPE_PARENT:
return "parent";
case CommonDataKinds.Relation.TYPE_PARTNER:
return "partner";
case CommonDataKinds.Relation.TYPE_REFERRED_BY:
return "referred by";
case CommonDataKinds.Relation.TYPE_RELATIVE:
return "relative";
case CommonDataKinds.Relation.TYPE_SISTER:
return "sister";
case CommonDataKinds.Relation.TYPE_SPOUSE:
return "spouse";
default:
return "other";
}
}
private int mapStringToPhoneType(String label) {
int phoneType;
switch (label) {
case "home":
phoneType = CommonDataKinds.Phone.TYPE_HOME;
break;
case "work":
phoneType = CommonDataKinds.Phone.TYPE_WORK;
break;
case "mobile":
phoneType = CommonDataKinds.Phone.TYPE_MOBILE;
break;
case "main":
phoneType = CommonDataKinds.Phone.TYPE_MAIN;
break;
case "work fax":
phoneType = CommonDataKinds.Phone.TYPE_FAX_WORK;
break;
case "home fax":
phoneType = CommonDataKinds.Phone.TYPE_FAX_HOME;
break;
case "pager":
phoneType = CommonDataKinds.Phone.TYPE_PAGER;
break;
case "work_pager":
phoneType = CommonDataKinds.Phone.TYPE_WORK_PAGER;
break;
case "work_mobile":
phoneType = CommonDataKinds.Phone.TYPE_WORK_MOBILE;
break;
default:
phoneType = CommonDataKinds.Phone.TYPE_CUSTOM;
break;
}
return phoneType;
}
private int mapStringToPostalAddressType(String label) {
int postalAddressType;
switch (label) {
case "home":
postalAddressType = CommonDataKinds.StructuredPostal.TYPE_HOME;
break;
case "work":
postalAddressType = CommonDataKinds.StructuredPostal.TYPE_WORK;
break;
default:
postalAddressType = CommonDataKinds.StructuredPostal.TYPE_CUSTOM;
break;
}
return postalAddressType;
}
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(Contacts.CONTENT_ITEM_TYPE);
intent.putExtra(Intents.Insert.IM_HANDLE, contact.getJid().toEscapedString());
intent.putExtra(Intents.Insert.IM_PROTOCOL, CommonDataKinds.Im.PROTOCOL_JABBER);
intent.putExtra("finishActivityOnSaveCompleted", true);
try {
ContactDetailsActivity.this.startActivityForResult(intent, 0);
} catch (ActivityNotFoundException e) {
ToastCompat.makeText(ContactDetailsActivity.this, R.string.no_application_found_to_view_contact, Toast.LENGTH_SHORT).show();
}
}
@Override
protected String getAdditionalFilters()
{
if (mPosition == 0)
{
return " AND " + Data.STARRED + " = " + 1;
}
else
{
Group group = (Group) mGroups.get(mPosition);
return " AND " + CommonDataKinds.GroupMembership.GROUP_ROW_ID + " = " + group.getId();
}
}
@Override
protected String getAdditionalFilters()
{
if (mPosition == 0)
{
return " AND " + Contacts.STARRED + " = " + 1;
}
else
{
return " AND " + Data.MIMETYPE + " = ? AND " + CommonDataKinds.GroupMembership.GROUP_ROW_ID + " = ?";
}
}
@Override
public Loader<Cursor> onCreateLoader(int loaderIndex, Bundle args) {
// Where the Contactables table excels is matching text queries,
// not just data dumps from Contacts db. One search term is used to query
// display name, email address and phone number. In this case, the query was extracted
// from an incoming intent in the handleIntent() method, via the
// intent.getStringExtra() method.
// BEGIN_INCLUDE(uri_with_query)
String query = args.getString(QUERY_KEY);
Uri uri = Uri.withAppendedPath(
CommonDataKinds.Contactables.CONTENT_FILTER_URI, query);
// END_INCLUDE(uri_with_query)
// BEGIN_INCLUDE(cursor_loader)
// Easy way to limit the query to contacts with phone numbers.
String selection =
CommonDataKinds.Contactables.HAS_PHONE_NUMBER + " = " + 1;
// Sort results such that rows for the same contact stay together.
String sortBy = CommonDataKinds.Contactables.LOOKUP_KEY;
return new CursorLoader(
mContext, // Context
uri, // URI representing the table/resource to be queried
null, // projection - the list of columns to return. Null means "all"
selection, // selection - Which rows to return (condition rows must match)
null, // selection args - can be provided separately and subbed into selection.
sortBy); // string specifying sort order
// END_INCLUDE(cursor_loader)
}
public static void updateContactPhoto(ContentResolver c, long rawContactId, Photo pic, boolean primary){
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
//insert new picture
try {
if(pic.data != null) {
//delete old picture
String where = ContactsContract.Data.RAW_CONTACT_ID + " = '" + rawContactId
+ "' AND " + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
Log.i(TAG, "Deleting picture: "+where);
ContentProviderOperation.Builder builder = ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI);
builder.withSelection(where, null);
operationList.add(builder.build());
builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
builder.withValue(ContactsContract.CommonDataKinds.Photo.RAW_CONTACT_ID, rawContactId);
builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, pic.data);
builder.withValue(ContactsContract.Data.SYNC2, String.valueOf(pic.timestamp));
builder.withValue(ContactsContract.Data.SYNC3, pic.url);
if (primary)
builder.withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
operationList.add(builder.build());
}
c.applyBatch(ContactsContract.AUTHORITY, operationList);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("ERROR:" , e.toString());
}
}
public static void removeContactLocations(Context c, Account account){
ContactsSyncAdapterService.mContentResolver = c.getContentResolver();
HashMap<String, ContactsSyncAdapterService.SyncEntry> localContacts = ContactsSyncAdapterService.getLocalContacts(account);
for (ContactsSyncAdapterService.SyncEntry s : localContacts.values()){
ContactsSyncAdapterService.mContentResolver.delete(ContactsContract.Data.CONTENT_URI, ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.Data.RAW_CONTACT_ID + " = " + s.raw_id, null);
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(Contacts.CONTENT_ITEM_TYPE);
intent.putExtra(Intents.Insert.IM_HANDLE, contact.getJid().toEscapedString());
intent.putExtra(Intents.Insert.IM_PROTOCOL, CommonDataKinds.Im.PROTOCOL_JABBER);
intent.putExtra("finishActivityOnSaveCompleted", true);
try {
ContactDetailsActivity.this.startActivityForResult(intent, 0);
} catch (ActivityNotFoundException e) {
Toast.makeText(ContactDetailsActivity.this, R.string.no_application_found_to_view_contact, Toast.LENGTH_SHORT).show();
}
}
public void removeNumberOrAddress(LinphoneNumberOrAddress noa) {
if (noa != null && noa.getOldValue() != null) {
if (isAndroidContact()) {
String select;
if (noa.isSIPAddress()) {
select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE + "' AND " + CommonDataKinds.SipAddress.SIP_ADDRESS + "=?";
} else {
select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND " + CommonDataKinds.Phone.NUMBER + "=?";
}
String[] args = new String[]{ getAndroidId(), noa.getOldValue() };
changesToCommit.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(select, args)
.build());
if (androidTagId != null && noa.isSIPAddress()) {
select = ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.DATA1 + "=?";
args = new String[] { androidTagId, noa.getOldValue() };
changesToCommit.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
.withSelection(select, args)
.build());
}
}
if (isLinphoneFriend()) {
if (noa.isSIPAddress()) {
if (!noa.getOldValue().startsWith("sip:")) {
noa.setOldValue("sip:" + noa.getOldValue());
}
}
LinphoneNumberOrAddress toRemove = null;
for (LinphoneNumberOrAddress address : addresses) {
if (noa.getOldValue().equals(address.getValue()) && noa.isSIPAddress() == address.isSIPAddress()) {
toRemove = address;
break;
}
}
if (toRemove != null) {
addresses.remove(toRemove);
}
}
}
}
public List<Phone> getPhoneNumbers(Context ctxt, long contactId, int flag) {
String id = Long.toString(contactId);
ArrayList<Phone> phones = new ArrayList<Phone>();
Cursor pCur;
if ((flag & ContactsWrapper.URI_NBR) > 0) {
pCur = ctxt.getContentResolver().query(
CommonDataKinds.Phone.CONTENT_URI,
null,
CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[] {
id
}, null);
while (pCur.moveToNext()) {
phones.add(new Phone(
pCur.getString(pCur
.getColumnIndex(CommonDataKinds.Phone.NUMBER)),
pCur.getString(pCur.getColumnIndex(CommonDataKinds.Phone.TYPE))
));
}
pCur.close();
}
// Add any custom IM named 'sip' and set its type to 'sip'
if ((flag & ContactsWrapper.URI_IM) > 0) {
pCur = ctxt.getContentResolver().query(
Data.CONTENT_URI,
null,
Data.CONTACT_ID + " = ? AND " + Data.MIMETYPE
+ " = ?",
new String[] {
id, CommonDataKinds.Im.CONTENT_ITEM_TYPE
}, null);
while (pCur.moveToNext()) {
// Could also use some other IM type but may be confusing. Are there
// phones with no 'custom' IM type?
if (pCur.getInt(pCur.getColumnIndex(CommonDataKinds.Im.PROTOCOL)) == CommonDataKinds.Im.PROTOCOL_CUSTOM) {
String proto = pCur.getString(pCur
.getColumnIndex(CommonDataKinds.Im.CUSTOM_PROTOCOL));
if (SipManager.PROTOCOL_SIP.equalsIgnoreCase(proto) || SipManager.PROTOCOL_CSIP.equalsIgnoreCase(proto)) {
phones.add(new Phone(pCur.getString(pCur
.getColumnIndex(CommonDataKinds.Im.DATA)), SipManager.PROTOCOL_SIP));
}
}
}
pCur.close();
}
// Add any SIP uri if android 9
if (Compatibility.isCompatible(9) && ((flag & ContactsWrapper.URI_SIP) > 0)) {
pCur = ctxt.getContentResolver().query(
Data.CONTENT_URI,
null,
Data.CONTACT_ID + " = ? AND " + Data.MIMETYPE
+ " = ?",
new String[] {
id, CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE
}, null);
while (pCur.moveToNext()) {
// Could also use some other IM type but may be confusing. Are
// there phones with no 'custom' IM type?
phones.add(new Phone(pCur.getString(pCur
.getColumnIndex(Data.DATA1)), SipManager.PROTOCOL_SIP));
}
pCur.close();
}
return (phones);
}
private String getContactDataCustomProtocolFilter(String protocol) {
return String.format(" %s='%s' AND %s=%s AND LOWER(%s)='%s'",
Data.MIMETYPE, CommonDataKinds.Im.CONTENT_ITEM_TYPE,
CommonDataKinds.Im.PROTOCOL, CommonDataKinds.Im.PROTOCOL_CUSTOM,
CommonDataKinds.Im.CUSTOM_PROTOCOL, protocol.toLowerCase());
}
@Override
public boolean isExternalPhoneNumber(Context context, Cursor cursor) {
String mimeType = cursor.getString(cursor.getColumnIndex(Data.MIMETYPE));
return CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equalsIgnoreCase(mimeType);
}
@Override
public boolean insertOrUpdateCSipUri(Context ctxt, long contactId, String uri) {
ContentResolver cr = ctxt.getContentResolver();
long rawContactId = -1;
Cursor c = cr.query(RawContacts.CONTENT_URI,
new String[]{RawContacts._ID},
RawContacts.CONTACT_ID + "=?",
new String[]{String.valueOf(contactId)}, null);
try {
if(c.moveToNext()) {
rawContactId = c.getLong(c.getColumnIndex(RawContacts._ID));
}
} catch (Exception e) {
Log.e(THIS_FILE, "Error while looping on contacts", e);
} finally {
c.close();
}
if(rawContactId != -1) {
String csipUri = SipUri.getCanonicalSipContact(uri, false);
// First try update
ContentValues cv = new ContentValues();
cv.put(CommonDataKinds.Im.DATA, csipUri);
Cursor cs = cr.query(Data.CONTENT_URI, new String[] {CommonDataKinds.Im._ID},
CommonDataKinds.Im.MIMETYPE + "=?"
+ " AND " + CommonDataKinds.Im.PROTOCOL + "=?"
+ " AND " + CommonDataKinds.Im.CUSTOM_PROTOCOL + "=?"
+ " AND " + CommonDataKinds.Im.RAW_CONTACT_ID + "=?", new String [] {
CommonDataKinds.Im.CONTENT_ITEM_TYPE,
Integer.toString(CommonDataKinds.Im.PROTOCOL_CUSTOM),
SipManager.PROTOCOL_CSIP,
Long.toString(rawContactId)
}, null);
if(cs != null) {
int count = cs.getCount();
cs.close();
if(count > 0) {
int updated = cr.update(Data.CONTENT_URI, cv,
CommonDataKinds.Im.MIMETYPE + "=?"
+ " AND " + CommonDataKinds.Im.PROTOCOL + "=?"
+ " AND " + CommonDataKinds.Im.CUSTOM_PROTOCOL + "=?"
+ " AND " + CommonDataKinds.Im.RAW_CONTACT_ID + "=?", new String [] {
CommonDataKinds.Im.CONTENT_ITEM_TYPE,
Integer.toString(CommonDataKinds.Im.PROTOCOL_CUSTOM),
SipManager.PROTOCOL_CSIP,
Long.toString(rawContactId)
});
Log.d(THIS_FILE, "Updated : " + updated);
}else {
cv.put(CommonDataKinds.Im.MIMETYPE, CommonDataKinds.Im.CONTENT_ITEM_TYPE);
cv.put(CommonDataKinds.Im.PROTOCOL, CommonDataKinds.Im.PROTOCOL_CUSTOM);
cv.put(CommonDataKinds.Im.CUSTOM_PROTOCOL, SipManager.PROTOCOL_CSIP);
cv.put(CommonDataKinds.Im.RAW_CONTACT_ID, rawContactId);
Uri insertedUri = cr.insert(Data.CONTENT_URI, cv);
if(insertedUri == null) {
return false;
}
Log.d(THIS_FILE, "Inserted : " + insertedUri.toString());
}
return true;
}
}
return false;
}
private void fillFromCursor() {
name = getString(CommonDataKinds.Relation.NAME);
final String label = getString(CommonDataKinds.Relation.LABEL);
final Integer type = getInt(CommonDataKinds.Relation.TYPE);
this.label = getLabelFromType(type, label);
}