|
@@ -3,6 +3,7 @@ package com.wdkl.ncs.android.middleware.utils;
|
|
|
import android.annotation.SuppressLint;
|
|
|
import android.content.ContentProviderOperation;
|
|
|
import android.content.ContentResolver;
|
|
|
+import android.content.ContentUris;
|
|
|
import android.content.ContentValues;
|
|
|
import android.content.Context;
|
|
|
import android.database.Cursor;
|
|
@@ -593,6 +594,104 @@ public class ContactHelper {
|
|
|
return te1;
|
|
|
}
|
|
|
|
|
|
+ public static void batchUpdate(Context context, List<WatchContactVO> contactVOS, Callback callback) {
|
|
|
+ synchronized (lock) {
|
|
|
+ batchDelContact(context);
|
|
|
+ if (callback != null) {
|
|
|
+ String text = "删除本地联系人完成...\n";
|
|
|
+ callback.onBack(text);
|
|
|
+ }
|
|
|
+
|
|
|
+ batchAddContact(context, contactVOS, callback);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //批量删除联系人
|
|
|
+ private static void batchDelContact(Context context) {
|
|
|
+ //得到ContentResolver对象
|
|
|
+ ContentResolver cr = context.getContentResolver();
|
|
|
+ //取得电话本中开始一项的光标
|
|
|
+ Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
|
|
|
+ if (cursor != null) {
|
|
|
+ ArrayList<ContentProviderOperation> operations = new ArrayList<>();
|
|
|
+ while (cursor.moveToNext()) {
|
|
|
+ // 取得联系人ID
|
|
|
+ long contactId = cursor.getLong(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID));
|
|
|
+ operations.add(ContentProviderOperation.newDelete(ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId))
|
|
|
+ .withYieldAllowed(true)
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+ cursor.close();
|
|
|
+
|
|
|
+ try {
|
|
|
+ if (operations.size() > 0) {
|
|
|
+ cr.applyBatch(ContactsContract.AUTHORITY, operations);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //批量添加联系人
|
|
|
+ private static void batchAddContact(Context context, List<WatchContactVO> contactVOS, Callback callback) {
|
|
|
+ if (contactVOS == null || contactVOS.size() == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ ArrayList<ContentProviderOperation> ops = new ArrayList<>();
|
|
|
+ int rawContactIndex;
|
|
|
+ for (int i = 0; i < contactVOS.size(); i++) {
|
|
|
+ String name = contactVOS.get(i).getName();
|
|
|
+ String number = contactVOS.get(i).getPhoneNumber();
|
|
|
+ if (TextUtils.isEmpty(name) || TextUtils.isEmpty(number)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ rawContactIndex = ops.size();
|
|
|
+
|
|
|
+ ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
|
|
|
+ .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
|
|
|
+ .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
|
|
|
+ .withYieldAllowed(true)
|
|
|
+ .build());
|
|
|
+
|
|
|
+ ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
|
|
|
+ .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactIndex)
|
|
|
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
|
|
|
+ .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
|
|
|
+ .withYieldAllowed(true)
|
|
|
+ .build());
|
|
|
+
|
|
|
+ ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
|
|
|
+ .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactIndex)
|
|
|
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
|
|
|
+ .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
|
|
|
+ .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
|
|
|
+ .withYieldAllowed(true)
|
|
|
+ .build());
|
|
|
+
|
|
|
+ if (callback != null) {
|
|
|
+ String text = "添加联系人: " + name + ", " + number;
|
|
|
+ callback.onBack(text);
|
|
|
+
|
|
|
+ float p = (float) (i+1) / (float) contactVOS.size();
|
|
|
+ int progress = (int) (p * 100);
|
|
|
+ callback.onUpdate(progress);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
|
|
|
+ if (callback != null) {
|
|
|
+ String text = "\n更新完成...";
|
|
|
+ callback.onBack(text);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
public interface Callback {
|
|
|
void onBack(String str);
|