|
@@ -9,15 +9,19 @@ import android.database.Cursor;
|
|
|
import android.net.Uri;
|
|
|
import android.provider.ContactsContract;
|
|
|
import android.telephony.TelephonyManager;
|
|
|
+import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
|
|
|
import com.wdkl.ncs.android.lib.base.BaseApplication;
|
|
|
+import com.wdkl.ncs.android.middleware.model.vo.WatchContactVO;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
|
|
|
public class ContactHelper {
|
|
|
-
|
|
|
+ private static final String TAG = "ContactHelper";
|
|
|
private static final Object lock = new Object();
|
|
|
+ private static boolean exitUpdate = false;
|
|
|
|
|
|
/**
|
|
|
* 插入手机号
|
|
@@ -26,7 +30,12 @@ public class ContactHelper {
|
|
|
* @param name
|
|
|
* @param phoneNumber
|
|
|
*/
|
|
|
- public static void insertContact(Context context, String name, String phoneNumber) {
|
|
|
+ public static void insertContact(Context context, String name, String phoneNumber, Callback callback) {
|
|
|
+ Log.d(TAG, "add contact: " + name + ", phone: " + phoneNumber);
|
|
|
+ if (callback != null) {
|
|
|
+ String text = "添加联系人: " + name + ", " + phoneNumber;
|
|
|
+ callback.onBack(text);
|
|
|
+ }
|
|
|
|
|
|
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
|
|
|
ContentResolver resolver = context.getContentResolver();
|
|
@@ -81,7 +90,7 @@ public class ContactHelper {
|
|
|
null, null, null); //从raw_contact表中返回display_name
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
int contact_id = cursor.getInt(0);
|
|
|
- Log.i("联系人ID", contact_id + "");
|
|
|
+ Log.i(TAG, contact_id + "");
|
|
|
cursor.close();
|
|
|
return contact_id;
|
|
|
}
|
|
@@ -95,6 +104,135 @@ public class ContactHelper {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
+ public static Integer checkPhoneNumExist(Context context, String phoneNum) {
|
|
|
+ //uri= content://com.android.contacts/data/phones/filter/#
|
|
|
+ Cursor cursor;
|
|
|
+ int id = -1;
|
|
|
+ try {
|
|
|
+ ContentResolver cr = context.getContentResolver();
|
|
|
+ cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
|
|
|
+ if (cursor != null) {
|
|
|
+ while (cursor.moveToNext()) {
|
|
|
+ boolean findContact = false;
|
|
|
+ // 取得联系人名字
|
|
|
+ int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
|
|
|
+ String name = cursor.getString(nameFieldColumnIndex);
|
|
|
+ // 取得联系人ID
|
|
|
+ String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
|
|
|
+ Cursor phoneCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
|
|
|
+ ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
|
|
|
+ //Log.d(TAG, "isPhoneNumbExist2: name=" + name + ", contactId=" + contactId);
|
|
|
+
|
|
|
+ // 取得电话号码(可能存在多个号码)
|
|
|
+ if (phoneCursor != null) {
|
|
|
+ while (phoneCursor.moveToNext()) {
|
|
|
+ String strPhoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
|
|
|
+ //Log.d(TAG, "get phoneNumber=" + strPhoneNumber + ", phoneNum=" + phoneNum);
|
|
|
+ if (!TextUtils.isEmpty(strPhoneNumber)) {
|
|
|
+ strPhoneNumber = strPhoneNumber.replaceAll(" ", "");
|
|
|
+ //过滤+86
|
|
|
+ strPhoneNumber = strPhoneNumber.replace("+86", "");
|
|
|
+ phoneNum = phoneNum.replace("+86", "");
|
|
|
+ if (phoneNum.equals(strPhoneNumber)) {
|
|
|
+ id = phoneCursor.getInt(phoneCursor.getColumnIndex(ContactsContract.Data.CONTACT_ID));
|
|
|
+ Log.d(TAG, "find contact id=" + id + ", phoneNumber=" + strPhoneNumber);
|
|
|
+ findContact = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ phoneCursor.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (findContact && id > 0) {
|
|
|
+ Log.e(TAG, "delete contact: " + name + ", id: " + id);
|
|
|
+ deleteContact(context, id); //删除原来号码通讯录
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cursor.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void checkContact(Context context, String phone) {
|
|
|
+ Cursor cursor = null;
|
|
|
+ try {
|
|
|
+ Uri phoneUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(phone));
|
|
|
+ ContentResolver resolver = context.getContentResolver();
|
|
|
+ cursor = resolver.query(phoneUri, new String[]{ContactsContract.CommonDataKinds.Phone._ID,
|
|
|
+ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.CONTACT_ID}, null, null, null);
|
|
|
+
|
|
|
+ Log.d(TAG, "check contact cursor: " + cursor);
|
|
|
+ if (cursor != null) {
|
|
|
+ while (cursor.moveToNext()) {
|
|
|
+ int id = cursor.getInt(0);
|
|
|
+ String name = cursor.getString(1);
|
|
|
+ int contactId = cursor.getInt(2);
|
|
|
+ Log.d(TAG, "find contact: " + name + ", contactId: " + contactId);
|
|
|
+ deleteContact(context, contactId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (cursor != null) {
|
|
|
+ cursor.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取手机联系人号码
|
|
|
+ */
|
|
|
+ private static void checkAndDeleteContact(Context context, String phone, Callback callback){
|
|
|
+ //得到ContentResolver对象
|
|
|
+ ContentResolver cr = context.getContentResolver();
|
|
|
+ //取得电话本中开始一项的光标
|
|
|
+ Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
|
|
|
+ if (cursor != null) {
|
|
|
+ while (cursor.moveToNext()) {
|
|
|
+ // 取得联系人名字
|
|
|
+ int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
|
|
|
+ String name = cursor.getString(nameFieldColumnIndex);
|
|
|
+ // 取得联系人ID
|
|
|
+ String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
|
|
|
+ Cursor phoneCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
|
|
|
+ ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
|
|
|
+ Log.d(TAG, "getPhoneNumber: nameFieldColumnIndex=" + nameFieldColumnIndex + ", name=" + name + ", contactId=" + contactId);
|
|
|
+
|
|
|
+ // 取得电话号码(可能存在多个号码)
|
|
|
+ if (phoneCursor != null) {
|
|
|
+ boolean delete = false;
|
|
|
+ while (phoneCursor.moveToNext()) {
|
|
|
+ String strPhoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
|
|
|
+ Log.d(TAG, "get phoneNumber=" + strPhoneNumber);
|
|
|
+ if (strPhoneNumber.equals(phone)) {
|
|
|
+ if (callback != null) {
|
|
|
+ String text = "找到相同号码联系人: " + name + ", " + strPhoneNumber;
|
|
|
+ callback.onBack(text);
|
|
|
+ }
|
|
|
+ delete = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ phoneCursor.close();
|
|
|
+
|
|
|
+ //删除联系人
|
|
|
+ if (delete) {
|
|
|
+ cr.delete(ContactsContract.Contacts.CONTENT_URI, ContactsContract.Contacts._ID + " =?", new String[]{contactId + ""});
|
|
|
+ cr.delete(ContactsContract.RawContacts.CONTENT_URI, ContactsContract.RawContacts.CONTACT_ID + " =?", new String[]{contactId + ""});
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cursor.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 根据姓名查找Contact_id
|
|
@@ -162,7 +300,7 @@ public class ContactHelper {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void setContact(Context context, String name, String phoneNumber) {
|
|
|
+ /*public static void setContact(Context context, String name, String phoneNumber) {
|
|
|
synchronized (lock) {
|
|
|
Integer contact_id = queryRawContactIdByName(context, name);
|
|
|
Integer contact_id2 = isPhoneNumbExist(context, phoneNumber);
|
|
@@ -175,9 +313,9 @@ public class ContactHelper {
|
|
|
}
|
|
|
// updateContact(context, contact_id, phoneNumber); //更新通讯录
|
|
|
}//通讯录姓名和号码没有变化,不用操作
|
|
|
- insertContact(context, name, phoneNumber);
|
|
|
+ insertContact(context, name, phoneNumber, null);
|
|
|
} else { //这个名字不存在通讯录中
|
|
|
- insertContact(context, name, phoneNumber);
|
|
|
+ insertContact(context, name, phoneNumber, null);
|
|
|
if (contact_id2 != null && contact_id2 > 0) { //电话号码确在通讯录中
|
|
|
deleteContact(context, contact_id2); //删除原来号码通讯录
|
|
|
}
|
|
@@ -185,6 +323,136 @@ public class ContactHelper {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+ }*/
|
|
|
+
|
|
|
+ public static void setContact(Context context, String name, String phoneNumber) {
|
|
|
+ synchronized (lock) {
|
|
|
+ if (TextUtils.isEmpty(phoneNumber)) {
|
|
|
+ Log.i(TAG, "setContact null");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ checkPhoneNumExist(context, phoneNumber);
|
|
|
+ Integer contact_id = queryRawContactIdByName(context, name);
|
|
|
+ Log.i(TAG, "setContact: " + contact_id + "");
|
|
|
+
|
|
|
+ if (contact_id != null && contact_id > 0) {
|
|
|
+ deleteContact(context, name);
|
|
|
+ insertContact(context, name, phoneNumber, null);
|
|
|
+ } else { //这个名字不存在通讯录中
|
|
|
+ insertContact(context, name, phoneNumber, null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //批量更新通讯录
|
|
|
+ public static void updateContacts(Context context, List<WatchContactVO> contactVOS, Callback callback) {
|
|
|
+ synchronized (lock) {
|
|
|
+ exitUpdate = false;
|
|
|
+ int index = 0;
|
|
|
+ int size = contactVOS.size();
|
|
|
+ //得到ContentResolver对象
|
|
|
+ ContentResolver cr = context.getContentResolver();
|
|
|
+ //取得电话本中开始一项的光标
|
|
|
+ Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
|
|
|
+
|
|
|
+ if (cursor != null) {
|
|
|
+ for (WatchContactVO contactVO : contactVOS) {
|
|
|
+ if (exitUpdate) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ index++;
|
|
|
+
|
|
|
+ //如果这个号码为空则不操作
|
|
|
+ if (TextUtils.isEmpty(contactVO.getPhoneNumber())) {
|
|
|
+ if (callback != null) {
|
|
|
+ String text = contactVO.getName() + ": 号码为空,不更新";
|
|
|
+ callback.onBack(text);
|
|
|
+
|
|
|
+ float p = (float) index / (float) size;
|
|
|
+ int progress = (int) (p * 100);
|
|
|
+ callback.onUpdate(progress);
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
+ do {
|
|
|
+ // 取得联系人名字
|
|
|
+ int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
|
|
|
+ String name = cursor.getString(nameFieldColumnIndex);
|
|
|
+ // 取得联系人ID
|
|
|
+ String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
|
|
|
+ Cursor phoneCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
|
|
|
+ ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
|
|
|
+ //Log.d(TAG, "getPhoneNumber: nameFieldColumnIndex=" + nameFieldColumnIndex + ", name=" + name + ", contactId=" + contactId);
|
|
|
+
|
|
|
+ // 取得电话号码(可能存在多个号码)
|
|
|
+ if (phoneCursor != null) {
|
|
|
+ boolean delete = false;
|
|
|
+ while (phoneCursor.moveToNext()) {
|
|
|
+ String strPhoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
|
|
|
+ //Log.d(TAG, "get phoneNumber=" + strPhoneNumber);
|
|
|
+ if (!TextUtils.isEmpty(strPhoneNumber)) {
|
|
|
+ strPhoneNumber = strPhoneNumber.replaceAll(" ", "");
|
|
|
+ //过滤+86
|
|
|
+ strPhoneNumber = strPhoneNumber.replace("+86", "");
|
|
|
+ String phoneNum = contactVO.getPhoneNumber().replace("+86", "");
|
|
|
+ //Log.e(TAG, "phone num compare: num1=" + strPhoneNumber + ", num2=" + phoneNum);
|
|
|
+ if (strPhoneNumber.equals(phoneNum)) {
|
|
|
+ if (callback != null) {
|
|
|
+ String text = "找到相同号码联系人: " + name + ", " + strPhoneNumber;
|
|
|
+ callback.onBack(text);
|
|
|
+ }
|
|
|
+ delete = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ phoneCursor.close();
|
|
|
+
|
|
|
+ //删除联系人
|
|
|
+ if (delete) {
|
|
|
+ cr.delete(ContactsContract.Contacts.CONTENT_URI, ContactsContract.Contacts._ID + " =?", new String[]{contactId + ""});
|
|
|
+ cr.delete(ContactsContract.RawContacts.CONTENT_URI, ContactsContract.RawContacts.CONTACT_ID + " =?", new String[]{contactId + ""});
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } while (cursor.moveToNext());
|
|
|
+ }
|
|
|
+
|
|
|
+ //新增联系人
|
|
|
+ insertContact(context, contactVO.getName(), contactVO.getPhoneNumber(), callback);
|
|
|
+
|
|
|
+ if (callback != null) {
|
|
|
+ float p = (float) index / (float) size;
|
|
|
+ int progress = (int) (p * 100);
|
|
|
+ callback.onUpdate(progress);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ cursor.close();
|
|
|
+
|
|
|
+ if (callback != null) {
|
|
|
+ String text = "更新完成!";
|
|
|
+ callback.onBack(text);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void exitUpdateContact() {
|
|
|
+ exitUpdate = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void updateContact(Context context, String name, String phoneNumber, Callback callback) {
|
|
|
+ synchronized (lock) {
|
|
|
+ //1.删除所有号码相同的联系人
|
|
|
+ checkAndDeleteContact(context, phoneNumber, callback);
|
|
|
+
|
|
|
+ //2.新增联系人
|
|
|
+ insertContact(context, name, phoneNumber, callback);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
@@ -229,4 +497,10 @@ public class ContactHelper {
|
|
|
@SuppressLint("MissingPermission") String te1 = tm.getLine1Number();//获取本机号码
|
|
|
return te1;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ public interface Callback {
|
|
|
+ void onBack(String str);
|
|
|
+ void onUpdate(int progress);
|
|
|
+ }
|
|
|
}
|