public ContactOperations addEvent(String event, int type, String label, boolean isPrimary, boolean isSuperPrimary) { mValues.clear(); if (!TextUtils.isEmpty(event)) { mValues.put(Event.START_DATE, event); mValues.put(Event.TYPE, type); mValues.put(Event.MIMETYPE, Event.CONTENT_ITEM_TYPE); if (!TextUtils.isEmpty(label)) { mValues.put(Event.LABEL, label); } if (isSuperPrimary) { mValues.put(Email.IS_SUPER_PRIMARY, 1); } if (isPrimary) { mValues.put(Email.IS_PRIMARY, 1); } addInsertOp(); } return this; }
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.^); } } }
/** * 为存在单值的域添加属性 * * @param contactPropMap * 包含联系人信息的 map * @param logicField * 域名称 * @param ops * ContentProviderOperation */ private void addSingleValueProperty(Map<String, Object> contactPropMap, String logicField, ArrayList<ContentProviderOperation> ops) { String fieldValue = (String) contactPropMap.get(logicField); if (XStringUtils.isEmptyString(fieldValue)) { return; } ContentProviderOperation.Builder builder = ContentProviderOperation .newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, LOGIC_FIELD_TO_CONTENT_TYPE_MAP.get(logicField)); if (logicField == LOGIC_FIELD_BIRTHDAY) { builder.withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY); } builder.withValue(LOGIC_FIELD_TO_NATIVE_FIELD_MAP.get(logicField), fieldValue); ops.add(builder.build()); }
private String getEventType(Integer type, String label) { if (type == null) { throw new InvalidCursorTypeException(); } switch (type) { case Event.TYPE_ANNIVERSARY: return "anniversary"; case Event.TYPE_OTHER: return "other"; case Event.TYPE_CUSTOM: return label; default: return "other"; } }
private void getEventValues(final String event, final int type, List<ContentValues> newContactValues) { if (event == null || event.length() < 11) { return; } ContentValues contentValues = new ContentValues(); contentValues.put(Data.MIMETYPE, Event.CONTENT_ITEM_TYPE); contentValues.put(Event.START_DATE, event.substring(0, 10)); contentValues.put(Event.TYPE, type); newContactValues.add(contentValues); }
public ContactOperations updateEvent(String event, String label, boolean isPrimary, boolean isSuperPrimary, Uri uri) { mValues.clear(); mValues.put(Event.START_DATE, event); mValues.put(Event.LABEL, label); mValues.put(Event.IS_PRIMARY, isPrimary ? 1 : 0); mValues.put(Event.IS_SUPER_PRIMARY, isSuperPrimary ? 1 : 0); addUpdateOp(uri); return this; }
public static int getAndroidEventType(EventType type) { switch (type) { case TYPE_CUSTOM: return Event.TYPE_CUSTOM; case TYPE_ANNIVERSARY: return Event.TYPE_ANNIVERSARY; case TYPE_BIRTHDAY: return Event.TYPE_BIRTHDAY; case TYPE_OTHER: return Event.TYPE_OTHER; default: return Event.TYPE_OTHER; } }
public static EventType getEventType(int androidType) { switch (androidType) { case Event.TYPE_CUSTOM: return EventType.TYPE_CUSTOM; case Event.TYPE_OTHER: return EventType.TYPE_OTHER; case Event.TYPE_ANNIVERSARY: return EventType.TYPE_ANNIVERSARY; case Event.TYPE_BIRTHDAY: return EventType.TYPE_BIRTHDAY; default: return EventType.TYPE_OTHER; } }
@Override public void cursorToHashMap(Map<String, Object> contactsMap, Cursor c) { // 获取当前行的 mimetype用于比较 String mimetype = c.getString(c .getColumnIndex(ContactsContract.Data.MIMETYPE)); if (StructuredName.CONTENT_ITEM_TYPE.equals(mimetype)) { contactsMap.put(LOGIC_FIELD_DISPLAYNAME, c.getString(c.getColumnIndex(StructuredName.DISPLAY_NAME))); nameQuery(contactsMap, c); } else if (Event.CONTENT_ITEM_TYPE.equals(mimetype)) { if (Event.TYPE_BIRTHDAY == c.getInt(c.getColumnIndex(Event.TYPE))) { contactsMap.put(LOGIC_FIELD_BIRTHDAY, c.getString(c.getColumnIndex(Event.START_DATE))); } } else if (Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) { contactsMap.put(LOGIC_FIELD_NICKNAME, c.getString(c.getColumnIndex(Nickname.NAME))); } else if (Note.CONTENT_ITEM_TYPE.equals(mimetype)) { contactsMap.put(LOGIC_FIELD_NOTE, c.getString(c.getColumnIndex(Note.NOTE))); } else { String logicField = LOGIC_FIELD_TO_CONTENT_TYPE_MAP .getKeyByValue(mimetype); if (LOGIC_CONTACT_MULTIPLE_VALUE_FIELDS.contains(logicField)) { multipleValueFieldQuery(contactsMap, c, logicField); } } }
private void fillFromCursor() { final Integer type = getInt(Event.TYPE); startDate = getString(Event.START_DATE); final String label = getString(Event.LABEL); this.type = getEventType(type, label); }
public static void removeBirthdays(Context c, long rawContactId){ 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 + "'"; c.getContentResolver().delete(ContactsContract.Data.CONTENT_URI, where, null); }
/** * 为存在单值的域修改属性 * * @param newContactMap * 包含新的要更新的联系人信息 * @param oldContactMap * android系统中保持的旧的联系人信息 * @param fieldName * 域名 * @param id * 此联系人的 id * @param rawId * 此联系人的 rawId * @param ops * ContentProviderOperation */ private void modifySingleValueProperty(Map<String, Object> newContactMap, Map<String, Object> oldContactMap, String fieldName, String id, int rawId, ArrayList<ContentProviderOperation> ops) { Object propValue = newContactMap.get(fieldName); ContentProviderOperation.Builder builder = null; String contentItemType = LOGIC_FIELD_TO_CONTENT_TYPE_MAP.get(fieldName); String nativeFieldName = LOGIC_FIELD_TO_NATIVE_FIELD_MAP.get(fieldName); Object oldPropValue = oldContactMap.get(fieldName); if (null == oldPropValue && null == propValue) { return; } if (null == oldPropValue && null != propValue) { // insert a new property builder = ContentProviderOperation .newInsert(ContactsContract.Data.CONTENT_URI) .withValue(ContactsContract.Data.RAW_CONTACT_ID, rawId) .withValue(ContactsContract.Data.MIMETYPE, contentItemType); if (fieldName == LOGIC_FIELD_BIRTHDAY) { builder.withValue(Event.TYPE, Event.TYPE_BIRTHDAY); } builder.withValue(nativeFieldName, propValue); } else if (null != oldPropValue && null == propValue) { // delete property builder = ContentProviderOperation.newDelete( ContactsContract.Data.CONTENT_URI).withSelection( ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", new String[] { String.valueOf(rawId), contentItemType }); } else { // update an existing property builder = ContentProviderOperation .newUpdate(ContactsContract.Data.CONTENT_URI); if (fieldName == LOGIC_FIELD_BIRTHDAY) { builder = builder.withSelection( ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=? AND " + Event.TYPE + "=?", new String[] { id, contentItemType, String.valueOf(Event.TYPE_BIRTHDAY) }); builder.withValue(Event.TYPE, Event.TYPE_BIRTHDAY).withValue( nativeFieldName, propValue); } else { builder = builder.withSelection( ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", new String[] { id, contentItemType }).withValue( nativeFieldName, propValue); } } ops.add(builder.build()); }
private List<ContentValues> getContactValues(final JSONObject contactProperties) throws JSONException { List<ContentValues> contactValues = new ArrayList<ContentValues>(); // Add the contact to the default group so it is shown in other apps // like the Contacts or People app ContentValues defaultGroupValues = new ContentValues(); defaultGroupValues.put(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE); defaultGroupValues.put(GroupMembership.GROUP_ROW_ID, mGroupId); contactValues.add(defaultGroupValues); // Create all the values that will be inserted into the new contact getNameValues(contactProperties.optJSONArray("name"), contactProperties.optJSONArray("givenName"), contactProperties.optJSONArray("familyName"), contactProperties.optJSONArray("honorificPrefix"), contactProperties.optJSONArray("honorificSuffix"), contactValues); getGenericValues(MIMETYPE_ADDITIONAL_NAME, CUSTOM_DATA_COLUMN, contactProperties.optJSONArray("additionalName"), contactValues); getNicknamesValues(contactProperties.optJSONArray("nickname"), contactValues); getAddressesValues(contactProperties.optJSONArray("adr"), contactValues); getPhonesValues(contactProperties.optJSONArray("tel"), contactValues); getEmailsValues(contactProperties.optJSONArray("email"), contactValues); //getPhotosValues(contactProperties.optJSONArray("photo"), contactValues); getGenericValues(Organization.CONTENT_ITEM_TYPE, Organization.COMPANY, contactProperties.optJSONArray("org"), contactValues); getGenericValues(Organization.CONTENT_ITEM_TYPE, Organization.TITLE, contactProperties.optJSONArray("jobTitle"), contactValues); getNotesValues(contactProperties.optJSONArray("note"), contactValues); getWebsitesValues(contactProperties.optJSONArray("url"), contactValues); getImsValues(contactProperties.optJSONArray("impp"), contactValues); getCategoriesValues(contactProperties.optJSONArray("category"), contactValues); getEventValues(contactProperties.optString("bday"), Event.TYPE_BIRTHDAY, contactValues); getEventValues(contactProperties.optString("anniversary"), Event.TYPE_ANNIVERSARY, contactValues); getCustomMimetypeValues(contactProperties.optString("sex"), MIMETYPE_SEX, contactValues); getCustomMimetypeValues(contactProperties.optString("genderIdentity"), MIMETYPE_GENDER_IDENTITY, contactValues); getGenericValues(MIMETYPE_KEY, CUSTOM_DATA_COLUMN, contactProperties.optJSONArray("key"), contactValues); return contactValues; }