瀏覽代碼

修改手动更新通讯录方式为批量操作

weizhengliang 3 年之前
父節點
當前提交
252424df47

+ 27 - 3
home/src/main/code/com/wdkl/ncs/android/component/home/activity/ContactUpdateActivity.kt

@@ -101,13 +101,12 @@ class ContactUpdateActivity: BaseActivity<WatchHomeActivityPresenter, ActivityCo
                 val str = "联系人名称: " + contact.name + ", 号码: " + contact.phoneNumber
                 val str = "联系人名称: " + contact.name + ", 号码: " + contact.phoneNumber
                 list.add(str)
                 list.add(str)
             }
             }
-            list.add("\n开始更新通讯录...")
+            list.add("\n开始更新...\n")
             adapter.data.addAll(list)
             adapter.data.addAll(list)
             adapter.notifyDataSetChanged()
             adapter.notifyDataSetChanged()
 
 
-
             Thread {
             Thread {
-                ContactHelper.updateContacts(applicationContext, contactsVOs, object : ContactHelper.Callback {
+                ContactHelper.batchUpdate(applicationContext, contactsVOs, object : ContactHelper.Callback {
                     override fun onBack(str: String) {
                     override fun onBack(str: String) {
                         runOnUiThread {
                         runOnUiThread {
                             adapter.data.add(str)
                             adapter.data.add(str)
@@ -130,6 +129,31 @@ class ContactUpdateActivity: BaseActivity<WatchHomeActivityPresenter, ActivityCo
                     }
                     }
                 })
                 })
             }.start()
             }.start()
+
+            /*Thread {
+                ContactHelper.updateContacts(applicationContext, contactsVOs, object : ContactHelper.Callback {
+                    override fun onBack(str: String) {
+                        runOnUiThread {
+                            adapter.data.add(str)
+                            if (adapter.data.size > 10) {
+                                update_contact_list.smoothScrollToPosition(adapter.itemCount - 1)
+                            }
+                            adapter.notifyDataSetChanged()
+                        }
+                    }
+
+                    override fun onUpdate(progress: Int) {
+                        runOnUiThread {
+                            progress_contact_update.setCurProgress(progress)
+                            if (progress == 100) {
+                                tv_contact_update_progress.setText("更新完成: " + progress + "%")
+                            } else {
+                                tv_contact_update_progress.setText("正在更新: " + progress + "%")
+                            }
+                        }
+                    }
+                })
+            }.start()*/
         }
         }
     }
     }
 
 

+ 99 - 0
middleware/src/main/code/com/wdkl/ncs/android/middleware/utils/ContactHelper.java

@@ -3,6 +3,7 @@ package com.wdkl.ncs.android.middleware.utils;
 import android.annotation.SuppressLint;
 import android.annotation.SuppressLint;
 import android.content.ContentProviderOperation;
 import android.content.ContentProviderOperation;
 import android.content.ContentResolver;
 import android.content.ContentResolver;
+import android.content.ContentUris;
 import android.content.ContentValues;
 import android.content.ContentValues;
 import android.content.Context;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.Cursor;
@@ -593,6 +594,104 @@ public class ContactHelper {
         return te1;
         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 {
     public interface Callback {
         void onBack(String str);
         void onBack(String str);