public static boolean UpdateContactsInfo(ContentResolver contactHelper, String display_name, String mobileNO, String officeNO, String email, String OgnizationTitle, String OgnizationData, String contactNote) { if (mobileNO.equals("")) { return false; } long contactId = -1;
// 성명과 휴대폰번호 기준으로 rawContactId 를 구한다. contactId = getContactIdFromNameAndNumber(contactHelper, display_name, mobileNO); if (contactId < 1) return false; // 데이터가 존재하지 않으면 바로 빠져나온다. long rawContactId = getrawIdFromContactId(contactHelper, contactId);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); Uri contactUri = ContactsContract.Data.CONTENT_URI; String where = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Contacts.Data.MIMETYPE + "=?";
// work number if (!officeNO.equals("")) { // 사무실 전화의 where 조건은 달라서 별도로 지정 String selectPhone = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'" + " AND " + Phone.TYPE + "=?"; String[] phoneNumParams = new String[]{String.valueOf(contactId), String.valueOf(Phone.TYPE_WORK)}; Cursor phoneNumCursor = contactHelper.query(contactUri, null, selectPhone, phoneNumParams, null); if (phoneNumCursor.getCount() > 0) { ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection(selectPhone, phoneNumParams) .withValue(Phone.NUMBER, officeNO) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK) .build()); } else { ContentValues cv = new ContentValues(); cv.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); cv.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); cv.put(ContactsContract.CommonDataKinds.Phone.DATA, officeNO); cv.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK); ops.add(android.content.ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI) .withValues(cv).build()); } }
// email if (!email.equals("")) { String[] emailParams = new String[]{String.valueOf(contactId), ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE}; Cursor emailCursor = contactHelper.query(contactUri, null, where, emailParams, null); if (emailCursor.getCount() > 0) { ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection(where, emailParams) .withValue(ContactsContract.CommonDataKinds.Email.DATA, email) .build()); } else { ContentValues cValues = new ContentValues(); cValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); cValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE); cValues.put(ContactsContract.CommonDataKinds.Email.DATA, email); ops.add(android.content.ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI) .withValues(cValues) .build()); } }
// 조직(organization) if (OgnizationData != "" || OgnizationTitle != "") { String[] orgParams = new String[]{String.valueOf(contactId), ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}; Cursor orgCursor = contactHelper.query(contactUri, null, where, orgParams, null); if (orgCursor.getCount() > 0) { ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection(where, orgParams) .withValue(ContactsContract.CommonDataKinds.Organization.DATA, OgnizationData) .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, OgnizationTitle) .build()); } else { ContentValues cValues = new ContentValues(); cValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); cValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE); cValues.put(ContactsContract.CommonDataKinds.Organization.DATA, OgnizationData); cValues.put(ContactsContract.CommonDataKinds.Organization.TITLE, OgnizationTitle); ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI) .withValues(cValues) .build()); } }
// note if (!contactNote.equals("")) { String[] noteParams = new String[]{String.valueOf(contactId), ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE}; Cursor noteCursor = contactHelper.query(contactUri, null, where, noteParams, null); if (noteCursor.getCount() > 0) { ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection(where, noteParams) .withValue(ContactsContract.CommonDataKinds.Note.NOTE, contactNote) .build()); } else { ContentValues cValues = new ContentValues(); cValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); cValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE); cValues.put(ContactsContract.CommonDataKinds.Note.NOTE, contactNote); ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI) .withValues(cValues) .build()); } }
try { // 연락처 제공자는 applyBatch()에서의 모든 작업을 하나의 트랜잭션으로서 수행 contactHelper.applyBatch(ContactsContract.AUTHORITY, ops); return true; } catch (RemoteException e) { e.printStackTrace(); } catch (OperationApplicationException e) { e.printStackTrace(); } return false; } |