|
@@ -0,0 +1,348 @@
|
|
|
|
+package com.wdkl.ncs.android.middleware.utils;
|
|
|
|
+
|
|
|
|
+import android.annotation.SuppressLint;
|
|
|
|
+import android.content.ContentProviderOperation;
|
|
|
|
+import android.content.ContentResolver;
|
|
|
|
+import android.content.ContentValues;
|
|
|
|
+import android.content.Context;
|
|
|
|
+import android.database.Cursor;
|
|
|
|
+import android.net.Uri;
|
|
|
|
+import android.provider.CallLog;
|
|
|
|
+import android.provider.ContactsContract;
|
|
|
|
+import android.telephony.TelephonyManager;
|
|
|
|
+import android.util.Log;
|
|
|
|
+
|
|
|
|
+import com.wdkl.ncs.android.lib.base.BaseApplication;
|
|
|
|
+import com.wdkl.ncs.android.middleware.common.Constants;
|
|
|
|
+import com.wdkl.ncs.android.middleware.model.vo.PhoneInteractionVO;
|
|
|
|
+
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Date;
|
|
|
|
+
|
|
|
|
+public class ContactHelper {
|
|
|
|
+ private static final String TAG = "ContactHelper";
|
|
|
|
+
|
|
|
|
+ //获取通话记录
|
|
|
|
+ public static void getCallLogs(Context context) {
|
|
|
|
+ Uri callUri = CallLog.Calls.CONTENT_URI;
|
|
|
|
+ Cursor cursor = null;
|
|
|
|
+ try {
|
|
|
|
+ cursor = context.getContentResolver().query(callUri, // 查询通话记录的URI
|
|
|
|
+ //columns, //数据库列选择 不选则获取所有列
|
|
|
|
+ null,
|
|
|
|
+ null,
|
|
|
|
+ null,
|
|
|
|
+ CallLog.Calls.DEFAULT_SORT_ORDER //按照时间逆序排列,最近打的最先显示
|
|
|
|
+ );
|
|
|
|
+ Log.i(TAG, "cursor count:" + cursor.getCount());
|
|
|
|
+ int i = 0;
|
|
|
|
+ while (cursor.moveToNext() && i < 20) {
|
|
|
|
+ i++;
|
|
|
|
+ String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)); //姓名
|
|
|
|
+ String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)); //号码
|
|
|
|
+ long dateLong = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE)); //获取通话日期
|
|
|
|
+ String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(dateLong));
|
|
|
|
+ //String time = new SimpleDateFormat("HH:mm").format(new Date(dateLong));
|
|
|
|
+ int duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));//获取通话时长,值为多少秒
|
|
|
|
+ int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE)); //获取通话类型:1.呼入 2.呼出 3.未接 4.voicemail 5.拒接
|
|
|
|
+ //String dayCurrent = new SimpleDateFormat("dd").format(new Date());
|
|
|
|
+ //String dayRecord = new SimpleDateFormat("dd").format(new Date(dateLong));
|
|
|
|
+ //String phone_account_address = cursor.getString(cursor.getColumnIndex("phone_account_address"));//本机号码可能获取不到(华为、oppo获取不到)
|
|
|
|
+ //String phone_account_id = cursor.getString(cursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID));//本机sim卡id,即ICCID
|
|
|
|
+ //String phone_account_hidden = cursor.getString(cursor.getColumnIndex("phone_account_hidden"));
|
|
|
|
+
|
|
|
|
+ Log.i(TAG, "Call log: " + "\n"
|
|
|
|
+ + "name: " + name + "\n"
|
|
|
|
+ + "date: " + date + "\n"
|
|
|
|
+ + "phone number: " + number + "\n"
|
|
|
|
+ + "duration: " + duration + "\n"
|
|
|
|
+ + "type: " + type
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ if (cursor != null) {
|
|
|
|
+ cursor.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static PhoneInteractionVO getLatestCallLog(final Context context) {
|
|
|
|
+ Uri callUri = CallLog.Calls.CONTENT_URI;
|
|
|
|
+ String[] columns = {CallLog.Calls.CACHED_NAME,
|
|
|
|
+ CallLog.Calls.NUMBER,
|
|
|
|
+ CallLog.Calls.DATE,
|
|
|
|
+ CallLog.Calls.DURATION,
|
|
|
|
+ CallLog.Calls.TYPE};
|
|
|
|
+ Cursor cursor = null;
|
|
|
|
+ PhoneInteractionVO interactionVO = null;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ cursor = context.getContentResolver().query(
|
|
|
|
+ callUri, // 查询通话记录的URI
|
|
|
|
+ columns, //数据库列选择 不选则获取所有列
|
|
|
|
+ null,
|
|
|
|
+ null,
|
|
|
|
+ CallLog.Calls.DEFAULT_SORT_ORDER //按照时间逆序排列,最近打的最先显示
|
|
|
|
+ );
|
|
|
|
+ Log.i(TAG, "cursor count:" + cursor.getCount());
|
|
|
|
+ if (cursor.moveToNext()) {
|
|
|
|
+ String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)); //姓名
|
|
|
|
+ String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)); //号码
|
|
|
|
+ long dateLong = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE)); //获取通话日期
|
|
|
|
+ String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(dateLong));
|
|
|
|
+ int duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));//获取通话时长,值为多少秒
|
|
|
|
+ int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE)); //获取通话类型:1.来电 2.去电 3.未接的来电 4.voicemail 5.拒接的来电
|
|
|
|
+
|
|
|
|
+ interactionVO = new PhoneInteractionVO();
|
|
|
|
+ if (type == CallLog.Calls.INCOMING_TYPE || type == CallLog.Calls.MISSED_TYPE || type == CallLog.Calls.REJECTED_TYPE) {
|
|
|
|
+ //接通的来电,未接的来电,拒接的来电
|
|
|
|
+ interactionVO.setFromDevicePhoneNumber(number);
|
|
|
|
+ interactionVO.setToDevicePhoneNumber(Constants.Companion.getPHONE_NUMBER());
|
|
|
|
+ interactionVO.setToId(Constants.Companion.getIds());
|
|
|
|
+ } else if (type == CallLog.Calls.OUTGOING_TYPE) {
|
|
|
|
+ //去电:包括接通的,自己取消的,对方拒接的
|
|
|
|
+ interactionVO.setFromDevicePhoneNumber(Constants.Companion.getPHONE_NUMBER());
|
|
|
|
+ interactionVO.setToDevicePhoneNumber(number);
|
|
|
|
+ interactionVO.setFromId(Constants.Companion.getIds());
|
|
|
|
+ }
|
|
|
|
+ interactionVO.setActionStart(dateLong);
|
|
|
|
+ interactionVO.setActionCallTime(duration);
|
|
|
|
+ interactionVO.setActionStatus(type);
|
|
|
|
+ //interactionVO.setActionAccept();
|
|
|
|
+ //结束时间需要减去延时的时间
|
|
|
|
+ interactionVO.setActionEnd(System.currentTimeMillis()-1200);
|
|
|
|
+
|
|
|
|
+ Log.i(TAG, "Call log: " + "\n"
|
|
|
|
+ + "name: " + name + "\n"
|
|
|
|
+ + "date: " + date + "\n"
|
|
|
|
+ + "phone number: " + number + "\n"
|
|
|
|
+ + "duration: " + duration + "\n"
|
|
|
|
+ + "type: " + type
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ if (cursor != null) {
|
|
|
|
+ cursor.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return interactionVO;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 插入手机号
|
|
|
|
+ *
|
|
|
|
+ * @param context
|
|
|
|
+ * @param name
|
|
|
|
+ * @param phoneNumber
|
|
|
|
+ */
|
|
|
|
+ public static void insertContact(Context context, String name, String phoneNumber) {
|
|
|
|
+
|
|
|
|
+ Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
|
|
|
|
+ ContentResolver resolver = context.getContentResolver();
|
|
|
|
+
|
|
|
|
+ ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
|
|
|
|
+ // 操作1.添加Google账号,这里值为null,表示不添加
|
|
|
|
+ ContentProviderOperation operation = ContentProviderOperation.newInsert(uri)
|
|
|
|
+ .withValue("account_name", null)// account_name:Google账号
|
|
|
|
+ .build();
|
|
|
|
+ // 操作2.添加data表中name字段
|
|
|
|
+ uri = Uri.parse("content://com.android.contacts/data");
|
|
|
|
+ ContentProviderOperation operation2 = ContentProviderOperation.newInsert(uri)
|
|
|
|
+ // 第二个参数int previousResult:表示上一个操作的位于operations的第0个索引,
|
|
|
|
+ // 所以能够将上一个操作返回的raw_contact_id作为该方法的参数
|
|
|
|
+ .withValueBackReference("raw_contact_id", 0)
|
|
|
|
+ .withValue("mimetype", "vnd.android.cursor.item/name")
|
|
|
|
+ .withValue("data2", name)
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ // 操作3.添加data表中phone字段
|
|
|
|
+ uri = Uri.parse("content://com.android.contacts/data");
|
|
|
|
+ ContentProviderOperation operation3 = ContentProviderOperation.newInsert(uri)
|
|
|
|
+ .withValueBackReference("raw_contact_id", 0)
|
|
|
|
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
|
|
|
|
+ //.withValue("mimetype", "vnd.android.cursor.item/phone_v2")
|
|
|
|
+ .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
|
|
|
|
+// .withValue("data2", "2")
|
|
|
|
+ .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber)
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ operations.add(operation);
|
|
|
|
+ operations.add(operation2);
|
|
|
|
+ operations.add(operation3);
|
|
|
|
+ try {
|
|
|
|
+ resolver.applyBatch("com.android.contacts", operations);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 判断某个手机号是否存在
|
|
|
|
+ */
|
|
|
|
+ public static Integer isPhoneNumbExist(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.CONTACT_ID},
|
|
|
|
+ null, null, null); //从raw_contact表中返回display_name
|
|
|
|
+ if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
+ int contact_id = cursor.getInt(0);
|
|
|
|
+ Log.i("联系人ID", contact_id + "");
|
|
|
|
+ cursor.close();
|
|
|
|
+ return contact_id;
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ if (cursor != null) {
|
|
|
|
+ cursor.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 根据姓名查找Contact_id
|
|
|
|
+ *
|
|
|
|
+ * @param context
|
|
|
|
+ * @param name
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static Integer queryRawContactIdByName(Context context, String name) {
|
|
|
|
+ //根据姓名求id
|
|
|
|
+ Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
|
|
|
|
+ ContentResolver resolver = context.getContentResolver();
|
|
|
|
+ Cursor cursor = resolver.query(uri, new String[]{ContactsContract.Data._ID}, "display_name=?", new String[]{name}, null);
|
|
|
|
+ if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
+ int id = cursor.getInt(0);
|
|
|
|
+ cursor.close();
|
|
|
|
+ return id;
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 更新通讯录,需要传入Contact_id
|
|
|
|
+ *
|
|
|
|
+ * @param context
|
|
|
|
+ * @param contact_id
|
|
|
|
+ * @param phoneNum
|
|
|
|
+ */
|
|
|
|
+ public static void updateContact(Context context, Integer contact_id, String phoneNum) {
|
|
|
|
+ try {
|
|
|
|
+ Uri uri = Uri.parse("content://com.android.contacts/data");//对data表的所有数据操作
|
|
|
|
+//
|
|
|
|
+//// ContentValues values = new ContentValues();
|
|
|
|
+//// values.put("data", phoneNum);
|
|
|
|
+//// resolver.update(uri, values, "mimetype=? and raw_contact_id=?", new String[]{"vnd.android.cursor.item/phone_v2",contact_id+""});
|
|
|
|
+// Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
|
|
|
|
+// ContentValues values = new ContentValues();
|
|
|
|
+// values.put("data1", phoneNum);
|
|
|
|
+// resolver.update(uri, values, "mimetype=? and raw_contact_id=?", new String[]{"vnd.android.cursor.item/phone_v2", contact_id + ""});
|
|
|
|
+
|
|
|
|
+ ContentResolver resolver = context.getContentResolver();
|
|
|
|
+ ContentValues values = new ContentValues();
|
|
|
|
+ values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNum);
|
|
|
|
+ values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
|
|
|
|
+
|
|
|
|
+ String Where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
|
|
|
|
+ String[] WhereParams = new String[]{contact_id + "", ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,};
|
|
|
|
+
|
|
|
|
+ resolver.update(uri, values, Where, WhereParams);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+// try {
|
|
|
|
+// Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
|
|
|
|
+// ContentResolver resolver = context.getContentResolver();
|
|
|
|
+// Cursor cursor = resolver.query(uri, new String[]{ ContactsContract.Data.DATA1, ContactsContract.Data.DATA2, ContactsContract.Data.DATA3}, "raw_contact_id=?", new String[]{contact_id + ""}, null);
|
|
|
|
+// if (cursor.moveToFirst()) {
|
|
|
|
+// int id = cursor.getInt(0);
|
|
|
|
+// String data1 = cursor.getString(1);
|
|
|
|
+// String data2 = cursor.getString(2);
|
|
|
|
+// Log.i("通讯录", "updateContact: id=" + id + ",data1=" + data1 + ",data2=" + data2);
|
|
|
|
+// }
|
|
|
|
+// cursor.close();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void setContact(Context context, String name, String phoneNumber) {
|
|
|
|
+
|
|
|
|
+ Integer contact_id = queryRawContactIdByName(context, name);
|
|
|
|
+ Integer contact_id2 = isPhoneNumbExist(context, phoneNumber);
|
|
|
|
+ Log.i("ContactId", "setContact: " + contact_id2 + ", name: " + name + ", phone: " + phoneNumber);
|
|
|
|
+ if (contact_id != null && contact_id > 0) {
|
|
|
|
+ deleteContact(context,name);
|
|
|
|
+ if (!(contact_id2 != null && contact_id2 > 0 && contact_id.equals(contact_id2))) { //非 通讯录中原有号码和姓名匹配的情况
|
|
|
|
+ if (contact_id2 != null && contact_id2 > 0) { //号码已经存在了
|
|
|
|
+ deleteContact(context, contact_id2); //删除号码
|
|
|
|
+ }
|
|
|
|
+// updateContact(context, contact_id, phoneNumber); //更新通讯录
|
|
|
|
+ }//通讯录姓名和号码没有变化,不用操作
|
|
|
|
+ insertContact(context, name, phoneNumber);
|
|
|
|
+ } else { //这个名字不存在通讯录中
|
|
|
|
+ insertContact(context, name, phoneNumber);
|
|
|
|
+ if(contact_id2!=null&&contact_id2>0){ //电话号码确在通讯录中
|
|
|
|
+ deleteContact(context, contact_id2); //删除原来号码通讯录
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 删除联系人
|
|
|
|
+ */
|
|
|
|
+ public static void deleteContact(Context context, String name) {
|
|
|
|
+
|
|
|
|
+ //根据姓名求id
|
|
|
|
+ Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
|
|
|
|
+ ContentResolver resolver = context.getContentResolver();
|
|
|
|
+ Cursor cursor = resolver.query(uri, new String[]{ContactsContract.Data._ID}, "display_name=?", new String[]{name}, null);
|
|
|
|
+ if (cursor == null)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
|
+ int id = cursor.getInt(0);
|
|
|
|
+ //根据id删除data中的相应数据
|
|
|
|
+ resolver.delete(uri, "display_name=?", new String[]{name});
|
|
|
|
+ uri = Uri.parse("content://com.android.contacts/data");
|
|
|
|
+ resolver.delete(uri, "raw_contact_id=?", new String[]{id + ""});
|
|
|
|
+ }
|
|
|
|
+ cursor.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 删除联系人
|
|
|
|
+ */
|
|
|
|
+ public static void deleteContact(Context context, Integer contact_id) {
|
|
|
|
+ //根据姓名求id
|
|
|
|
+ Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
|
|
|
|
+ ContentResolver resolver = context.getContentResolver();
|
|
|
|
+ //根据id删除data中的相应数据
|
|
|
|
+ resolver.delete(uri, "_id=?", new String[]{contact_id + ""});
|
|
|
|
+ uri = Uri.parse("content://com.android.contacts/data");
|
|
|
|
+ resolver.delete(uri, "raw_contact_id=?", new String[]{contact_id + ""});
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String getPhoneNumber(){
|
|
|
|
+ TelephonyManager tm = (TelephonyManager) BaseApplication.appContext.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
|
|
|
|
+ @SuppressLint("MissingPermission") String te1 = tm.getLine1Number();//获取本机号码
|
|
|
|
+ return te1;
|
|
|
|
+ }
|
|
|
|
+}
|