version:1.3.4
fix:修复闪退 update:优化联系人添加
This commit is contained in:
391
app/src/main/java/com/xxpatx/os/utils/ContactsUtils.java
Normal file
391
app/src/main/java/com/xxpatx/os/utils/ContactsUtils.java
Normal file
@@ -0,0 +1,391 @@
|
||||
package com.xxpatx.os.utils;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.ContentUris;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.provider.ContactsContract;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.request.target.SimpleTarget;
|
||||
import com.bumptech.glide.request.transition.Transition;
|
||||
import com.xxpatx.os.R;
|
||||
import com.xxpatx.os.bean.Contact;
|
||||
import com.xxpatx.os.bean.ContactId;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.rxjava3.annotations.NonNull;
|
||||
import io.reactivex.rxjava3.core.Observable;
|
||||
import io.reactivex.rxjava3.core.ObservableEmitter;
|
||||
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
|
||||
import io.reactivex.rxjava3.core.Observer;
|
||||
import io.reactivex.rxjava3.disposables.Disposable;
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||
|
||||
public class ContactsUtils {
|
||||
private static final String TAG = "ContactsUtils";
|
||||
|
||||
|
||||
public static void saveContactPhone(Context context, List<Contact> contactList) {
|
||||
Log.e(TAG, "saveContactPhone: ");
|
||||
// List<ContactId> contactIdList = ContactsUtils.getLocalContacts(context);
|
||||
Observable.create(new ObservableOnSubscribe<Long>() {
|
||||
@Override
|
||||
public void subscribe(@NonNull ObservableEmitter<Long> emitter) throws Throwable {
|
||||
for (Contact contact : contactList) {
|
||||
if (TextUtils.isEmpty(contact.getMobile())) {
|
||||
continue;
|
||||
}
|
||||
if (ContactsUtils.isExist(context, contact.getMobile())) {
|
||||
long rawContactId = ContactsUtils.getContactId(context, contact.getMobile());
|
||||
Glide.with(context).asBitmap().load(contact.getAvatar()).override(200, 200).into(new SimpleTarget<Bitmap>() {
|
||||
@Override
|
||||
public void onResourceReady(@androidx.annotation.NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
ContentValues nameValues = new ContentValues();
|
||||
//add Name
|
||||
nameValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
|
||||
nameValues.put(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/name");
|
||||
nameValues.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, contact.getName());
|
||||
resolver.update(ContactsContract.Data.CONTENT_URI, nameValues, "raw_contact_id=?" + rawContactId, new String[]{ContactsContract.Data.CONTACT_ID});
|
||||
|
||||
ContentValues photoValues = new ContentValues();
|
||||
photoValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
resource.compress(Bitmap.CompressFormat.PNG, 100, out);
|
||||
photoValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
|
||||
photoValues.put(ContactsContract.CommonDataKinds.Photo.PHOTO, out.toByteArray());
|
||||
resolver.update(ContactsContract.Data.CONTENT_URI, photoValues, "raw_contact_id=?" + rawContactId, new String[]{ContactsContract.Data.CONTACT_ID});
|
||||
|
||||
ContentValues phoneValues = new ContentValues();
|
||||
phoneValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
|
||||
phoneValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
|
||||
phoneValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.getMobile());
|
||||
phoneValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
|
||||
resolver.update(ContactsContract.Data.CONTENT_URI, phoneValues, "raw_contact_id=?" + rawContactId, new String[]{ContactsContract.Data.CONTACT_ID});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
ContentValues values = new ContentValues();
|
||||
long rawContactId = ContentUris.parseId(context.getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, values));
|
||||
Glide.with(context).asBitmap().load(contact.getAvatar()).override(200, 200).into(new SimpleTarget<Bitmap>() {
|
||||
@Override
|
||||
public void onResourceReady(@androidx.annotation.NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
ContentValues nameValues = new ContentValues();
|
||||
//add Name
|
||||
nameValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
|
||||
nameValues.put(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/name");
|
||||
nameValues.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, contact.getName());
|
||||
resolver.insert(ContactsContract.Data.CONTENT_URI, nameValues);
|
||||
|
||||
ContentValues photoValues = new ContentValues();
|
||||
photoValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
resource.compress(Bitmap.CompressFormat.PNG, 100, out);
|
||||
photoValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
|
||||
photoValues.put(ContactsContract.CommonDataKinds.Photo.PHOTO, out.toByteArray());
|
||||
resolver.insert(ContactsContract.Data.CONTENT_URI, photoValues);
|
||||
|
||||
ContentValues phoneValues = new ContentValues();
|
||||
phoneValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
|
||||
phoneValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
|
||||
phoneValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.getMobile());
|
||||
phoneValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
|
||||
resolver.insert(ContactsContract.Data.CONTENT_URI, phoneValues);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
emitter.onNext(1L);
|
||||
}
|
||||
})
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<Long>() {
|
||||
@Override
|
||||
public void onSubscribe(@NonNull Disposable d) {
|
||||
Log.e("saveContactPhone", "onSubscribe: ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(@NonNull Long aLong) {
|
||||
Log.e("saveContactPhone", "onNext: " + aLong);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(@NonNull Throwable e) {
|
||||
Log.e("saveContactPhone", "onError: " + e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
Log.e("saveContactPhone", "onComplete: ");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加联系人信息
|
||||
*/
|
||||
public static void insertConstacts(Context context, String name, String phone, List<String> list) {
|
||||
long contactId = getContactId(context, name);
|
||||
if (contactId == -1) {
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
|
||||
//插入raw_contacts表,并获取_id属性
|
||||
ContentValues nameValues = new ContentValues();
|
||||
long rawContactId = ContentUris.parseId(resolver.insert(ContactsContract.RawContacts.CONTENT_URI, nameValues));
|
||||
|
||||
//插入data表
|
||||
//add Name
|
||||
nameValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
|
||||
nameValues.put(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/name");
|
||||
nameValues.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, name);
|
||||
resolver.insert(ContactsContract.Data.CONTENT_URI, nameValues);
|
||||
|
||||
|
||||
try {
|
||||
//写入头像
|
||||
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.default_avatar);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
|
||||
ContentValues photoValues = new ContentValues();
|
||||
photoValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
|
||||
photoValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
|
||||
photoValues.put(ContactsContract.CommonDataKinds.Photo.PHOTO, out.toByteArray());
|
||||
context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, photoValues);
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
ContentValues phoneValues = new ContentValues();
|
||||
//写入手机号码
|
||||
phoneValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
|
||||
phoneValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
|
||||
phoneValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phone);
|
||||
phoneValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
|
||||
//插入data表
|
||||
context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, phoneValues);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断某个手机号是否存在
|
||||
*/
|
||||
public static boolean isThePhoneExist(Context context, String phoneNum) {
|
||||
//uri= content://com.android.contacts/data/phones/filter/#
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/" + phoneNum);
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
cursor = resolver.query(uri, new String[]{ContactsContract.Data.DISPLAY_NAME},
|
||||
null, null, null); //从raw_contact表中返回display_name
|
||||
for (String columnName : cursor.getColumnNames()) {
|
||||
Log.e("isThePhoneExist: ", columnName);
|
||||
}
|
||||
while (cursor.moveToNext()) {
|
||||
for (String columnName : cursor.getColumnNames()) {
|
||||
Log.e("isThePhoneExist: ", columnName);
|
||||
Log.e("isThePhoneExist: ", cursor.getColumnIndex(columnName) + "");
|
||||
if (cursor.getColumnIndex(columnName) != -1) {
|
||||
Log.e("isThePhoneExist: getString = ", " - " + cursor.getString(cursor.getColumnIndex(columnName)));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cursor.moveToFirst()) {
|
||||
//Log.i(TAG, "name=" + cursor.getString(0) + " , phoneNum = " + phoneNum);
|
||||
cursor.close();
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
//Log.i(TAG, "163 e =" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有联系人信息
|
||||
*/
|
||||
// public static List<SysContactsListBean> getAllSysContacts(Context context) {
|
||||
// List<SysContactsListBean> list = new ArrayList<>();
|
||||
// try {
|
||||
// ContentResolver resolver = context.getContentResolver();
|
||||
// SysContactsListBean sysContactsListBean;
|
||||
// Uri uri = Uri.parse("content://com.android.contacts/data/phones");
|
||||
//
|
||||
// Cursor cursor1 = resolver.query(uri,
|
||||
// new String[]{ContactsContract.Data.RAW_CONTACT_ID,
|
||||
// ContactsContract.Data.DISPLAY_NAME,
|
||||
// ContactsContract.CommonDataKinds.Phone.NUMBER},
|
||||
// null, null, null);
|
||||
//
|
||||
// while (cursor1.moveToNext()) {
|
||||
// sysContactsListBean = new SysContactsListBean();
|
||||
// sysContactsListBean.setCustomerId(cursor1.getLong(0));
|
||||
// sysContactsListBean.setCustomerName(cursor1.getString(1));
|
||||
// sysContactsListBean.setPhoneNumber(cursor1.getString(2));
|
||||
// list.add(sysContactsListBean);
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
//
|
||||
// if (!CollectorUtils.isEmpty(list)) {
|
||||
// Collections.sort(list, new Comparator<SysContactsListBean>() {
|
||||
// @Override
|
||||
// public int compare(SysContactsListBean o1, SysContactsListBean o2) {
|
||||
// return o2.getCustomerName().compareTo(o1.getCustomerName());
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// return list;
|
||||
// }
|
||||
public static void getContacts(Context context) {
|
||||
//https://blog.csdn.net/luofeixiongsix/article/details/48849511
|
||||
//查询raw_contacts表获得联系人
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
// Uri uri = ContactsContract.Data.CONTENT_URI;//content://com.android.contacts/data 有电话+86
|
||||
// Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;//content://com.android.contacts/data/phones 有电话+86
|
||||
// Uri uri = ContactsContract.RawContacts.CONTENT_URI;//content://com.android.contacts/raw_contacts 无电话
|
||||
Uri uri = ContactsContract.Contacts.CONTENT_URI;//content://com.android.contacts/contacts 无电话
|
||||
|
||||
Log.e(TAG, "getContacts: " + uri.toString());
|
||||
//查询联系人
|
||||
Cursor cursor1 = resolver.query(ContactsContract.Data.CONTENT_URI, null, null, null, null);
|
||||
Log.e("getContacts: cursor1 = ", Arrays.toString(cursor1.getColumnNames()));
|
||||
Cursor cursor2 = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
|
||||
Log.e("getContacts: cursor2 = ", Arrays.toString(cursor1.getColumnNames()));
|
||||
Cursor cursor3 = resolver.query(ContactsContract.RawContacts.CONTENT_URI, null, null, null, null);
|
||||
Log.e("getContacts: cursor3 = ", Arrays.toString(cursor1.getColumnNames()));
|
||||
Cursor cursor4 = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
|
||||
Log.e("getContacts: cursor4 = ", Arrays.toString(cursor1.getColumnNames()));
|
||||
|
||||
// while (cursor.moveToNext()) {
|
||||
// String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
|
||||
// String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
|
||||
// Log.e(TAG, "getContacts: 联系人:" + name);
|
||||
// Log.e(TAG, "getContacts: 电话:" + number);
|
||||
// }
|
||||
// for (String columnName : cursor.getColumnNames()) {
|
||||
// Log.e("getContacts: ", columnName);
|
||||
// }
|
||||
// while (cursor.moveToNext()) {
|
||||
// for (String columnName : cursor.getColumnNames()) {
|
||||
// Log.e("getContacts: ", cursor.getColumnIndex(columnName) + "\t" + columnName);
|
||||
// if (cursor.getColumnIndex(columnName) != -1) {
|
||||
// Log.e("getContacts: getString =", " " + cursor.getString(cursor.getColumnIndex(columnName)));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// cursor.close();
|
||||
}
|
||||
|
||||
public static List<ContactId> getLocalContacts(Context context) {
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
Uri uri = ContactsContract.Contacts.CONTENT_URI;
|
||||
Cursor cursor = resolver.query(uri, null, null, null, null);
|
||||
List<ContactId> contactIds = new ArrayList<>();
|
||||
while (cursor.moveToNext()) {
|
||||
long id = cursor.getLong(cursor.getColumnIndexOrThrow("name_raw_contact_id"));
|
||||
int in_phone = cursor.getInt(cursor.getColumnIndexOrThrow("indicate_phone_or_sim_contact"));
|
||||
String display_name = cursor.getString(cursor.getColumnIndexOrThrow("display_name"));
|
||||
String photo_uri = cursor.getString(cursor.getColumnIndexOrThrow("photo_uri"));
|
||||
|
||||
ContactId contactId = new ContactId(id, in_phone, display_name, photo_uri);
|
||||
contactIds.add(contactId);
|
||||
}
|
||||
cursor.close();
|
||||
List<ContactId> filter = contactIds.stream().filter(new Predicate<ContactId>() {
|
||||
@Override
|
||||
public boolean test(ContactId contactId) {
|
||||
return contactId.getInPhone() == -1;
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
Log.e(TAG, "getLocalContacts: " + filter);
|
||||
return filter;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断某个手机号是否存在
|
||||
*/
|
||||
public static boolean isExist(Context context, String phoneNum) {
|
||||
//uri= content://com.android.contacts/data/phones/filter/#
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/" + phoneNum);
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
cursor = resolver.query(uri, new String[]{ContactsContract.Data.DISPLAY_NAME},
|
||||
null, null, null); //从raw_contact表中返回display_name
|
||||
if (cursor.moveToFirst()) {
|
||||
cursor.close();
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param context
|
||||
* @param phoneNum
|
||||
* @return 获取联系人id
|
||||
*/
|
||||
public static long getContactId(Context context, String phoneNum) {
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/" + phoneNum);
|
||||
ContentResolver resolver = context.getContentResolver();
|
||||
cursor = resolver.query(uri, null, null, null, null); //从raw_contact表中返回display_name
|
||||
if (cursor.moveToFirst()) {
|
||||
for (String columnName : cursor.getColumnNames()) {
|
||||
Log.e("getContactId: ", cursor.getColumnIndex(columnName) + "\t" + columnName);
|
||||
if (cursor.getColumnIndex(columnName) != -1) {
|
||||
Log.e("getContactId: getString =", " " + cursor.getString(cursor.getColumnIndex(columnName)));
|
||||
}
|
||||
}
|
||||
return cursor.getLong(cursor.getColumnIndexOrThrow(ContactsContract.Data.CONTACT_ID));
|
||||
}
|
||||
cursor.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user