import android.content.ContentProviderOperation; import android.content.ContentResolver; import android.content.Context; import android.content.OperationApplicationException; import android.database.Cursor; import android.graphics.Bitmap; import android.os.RemoteException; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Email; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.util.Log; import android.widget.Toast;
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList;
public class Contacts { public Contacts(){ } // 전화번호부에 존재하는 데이터인지 여부 검사 // 표시 이름과 휴대폰번호를 기준으로 ContactId 구하기 public long ContactsIDExistCheck(ContentResolver cr, String display_name, String number) { long rawContactId = -1;
Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); String[] projection = { PhoneLookup._ID, PhoneLookup.TYPE, PhoneLookup.DISPLAY_NAME }; Cursor cursor = null; try { cursor = cr.query(contactUri, projection, null, null, null); if (cursor.moveToFirst()) { do { String PhoneName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME)); if (display_name.equals(PhoneName)) { rawContactId = cursor.getLong(cursor.getColumnIndex(PhoneLookup._ID)); System.out.println(" ContactId = " + rawContactId + " 성명 : " + PhoneName); } } while (cursor.moveToNext()); } } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) { cursor.close(); } } return rawContactId; } public void ContactsIDdelete(ContentResolver cr, Context context, Integer contactId){ // 전화번호부 삭제 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); //remove contact from raw_contact table ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI). withSelection(ContactsContract.RawContacts.CONTACT_ID + "=?", new String[]{String.valueOf(contactId)}). build()); try { cr.applyBatch(ContactsContract.AUTHORITY, ops); } catch (RemoteException e) { e.printStackTrace(); } catch (OperationApplicationException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public void ContactsIDinsert(ContentResolver cr, Context context, String strName, String strMobileNO) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, strName) .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) .withValue(Phone.NUMBER, strMobileNO) .withValue(Phone.TYPE, Phone.TYPE_MOBILE) .build());
try { cr.applyBatch(ContactsContract.AUTHORITY, ops); Toast.makeText(context, "연락처가 저장되었습니다.", Toast.LENGTH_LONG).show(); } catch (RemoteException e) { e.printStackTrace(); } catch (OperationApplicationException e) { e.printStackTrace(); } catch (Exception e) { Log.e("ContactsAdder", "Exceptoin encoutered while inserting contact: " + e); } } public void ContactsIDinsert(ContentResolver cr, Context context, String strName, String strMobileNO, String strOfficeNO, String strEmail, String strPhoto) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, strName) .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) .withValue(Phone.NUMBER, strMobileNO) .withValue(Phone.TYPE, Phone.TYPE_MOBILE) .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) .withValue(Phone.NUMBER, strOfficeNO) .withValue(Phone.TYPE, Phone.TYPE_WORK) .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE) .withValue(Email.DATA, strEmail) .withValue(Email.TYPE, Email.TYPE_WORK) .build());
MemoryCache memoryCache = new MemoryCache(); Bitmap cachedImage = memoryCache.get(strPhoto); System.out.println("cachedImage ===" + cachedImage); if (cachedImage != null) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); cachedImage.compress(Bitmap.CompressFormat.JPEG, 60, bos);
byte[] bytes = bos.toByteArray();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Photo.DATA15, bytes) .build()); try { bos.flush(); bos.close(); } catch (IOException e) { e.printStackTrace(); } }
try { cr.applyBatch(ContactsContract.AUTHORITY, ops); Toast.makeText(context, "연락처가 저장되었습니다.", Toast.LENGTH_LONG).show(); } catch (RemoteException e) { e.printStackTrace(); } catch (OperationApplicationException e) { e.printStackTrace(); } catch (Exception e) { Log.e("ContactsAdder", "Exceptoin encoutered while inserting contact: " + e); } } }
|