Android实现分批加载手机中所有联系人数据

此文记录联系人数据帮助类,包括联系人数据的分批加载功能。

在Android系统中,想要获取联系人数据,首先我们需要了解联系人数据库的结构,通过系统提供的ContactProvider来操作联系人数据。

关于ContactProvider

android.provider.ContactsContract类是联系人数据协定类,大家可参考了解

其源代码路劲如下:
\frameworks\base\core\java\android\provider\ContactsContract.java

通过上面的信息帮助看懂了Android系统联系人数据结构,那么我们就可以更好的理解并使用以下帮助类代码了

ContactsDataLoader.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.ContactsContract.CommonDataKinds.Nickname;
import android.provider.ContactsContract.CommonDataKinds.Note;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.CommonDataKinds.Website;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.util.Base64;
import android.util.Log;

import com.transage.privatespace.bean.ContactDataType;
import com.transage.privatespace.bean.ContactInfo;

import org.json.JSONException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
* 手机联系人加载器帮助类
* Created by yanjie.xu on 2017/8/24.
*/

public class ContactsDataLoader {
public static final String TAG = "ContactsDataLoader";
private static Context mContext;
private List<ContactInfo> mContactInfos = new ArrayList<>();

public ContactsDataLoader(Context context) {
this.mContext = context;
}

// ContactsContract.Contacts.CONTENT_URI= content://com.android.contacts/contacts;
// ContactsContract.Data.CONTENT_URI = content://com.android.contacts/data;

/**
* 分页查询系统联系人信息
*
* @param pageSize 每页最大的数目
* @param currentOffset 当前的偏移量
* @return
*/
public List<ContactInfo> getContactsByPage(int pageSize, int currentOffset) {
// String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
// ContactsContract.CommonDataKinds.Phone.DATA1, "sort_key"};
// Cursor cursor = mContext.getContentResolver().query(uri, projection, null, null,
// "sort_key COLLATE LOCALIZED asc limit " + pageSize + " offset " + currentOffset);
mContactInfos.clear();
Cursor cur = mContext.getContentResolver().query(Contacts.CONTENT_URI, null, null, null,
" sort_key desc limit " + pageSize + " offset " + currentOffset);
if (cur.moveToFirst()) {
do {
ContactInfo info = new ContactInfo();
// 获取联系人id号
int id = cur.getInt(cur.getColumnIndex(Contacts._ID));
// 获取联系人姓名
String displayName = cur.getString(cur.getColumnIndex(Contacts.DISPLAY_NAME));
//ContactInfo info = new ContactInfo(displayName);// 初始化联系人信息
// 查看联系人有多少电话号码, 如果没有返回0
int phoneCount = cur.getInt(cur.getColumnIndex(Contacts.HAS_PHONE_NUMBER));
//得到联系人头像ID
int photoid = cur.getInt(cur.getColumnIndex(Contacts.PHOTO_ID));
//头像文件id
int photoFileId = cur.getInt(cur.getColumnIndex(Contacts.PHOTO_FILE_ID));
//photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
if (photoid > 0) {
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(mContext.getContentResolver(), uri);
//contactPhoto = BitmapFactory.decodeStream(input);
info.setPhotoFileId(photoFileId);
info.setPhotoData(input2byte(input));
} else {
//info.setPhoto(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_person_black_128dp));
}
info.setContactId(id);
info.setDisplayName(displayName);
info.setHasPhoneNumber(phoneCount);

getRawContact(id, info);
Log.i(TAG, "info = " + info.toString());
mContactInfos.add(info);
} while (cur.moveToNext());
cur.close();
}

return mContactInfos;
}

/**
* 获得系统联系人的所有记录数目
*
* @return
*/
public int getAllCounts() {
int num = 0;
// 使用ContentResolver查找联系人数据
Cursor cursor = mContext.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null,
null, null);

// 遍历查询结果,获取系统中所有联系人
while (cursor.moveToNext()) {
num++;
}
cursor.close();
return num;
}

//获取手机中所有联系人数据
public List<ContactInfo> getContacts() {
Cursor cur = mContext.getContentResolver().query(Contacts.CONTENT_URI, null, null, null, null);
if (cur.moveToFirst()) {
do {
// 初始化联系人信息
ContactInfo info = new ContactInfo();
// 获取联系人id号
int id = cur.getInt(cur.getColumnIndex(Contacts._ID));
// 获取联系人姓名
String displayName = cur.getString(cur.getColumnIndex(Contacts.DISPLAY_NAME));
// 查看联系人有多少电话号码, 如果没有返回0
int phoneCount = cur.getInt(cur.getColumnIndex(Contacts.HAS_PHONE_NUMBER));
//得到联系人头像ID
int photoid = cur.getInt(cur.getColumnIndex(Contacts.PHOTO_ID));
//头像文件id
int photoFileId = cur.getInt(cur.getColumnIndex(Contacts.PHOTO_FILE_ID));
//photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
if (photoid > 0) {
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(mContext.getContentResolver(), uri);
// contactPhoto = BitmapFactory.decodeStream(input);
info.setPhotoFileId(photoFileId);
info.setPhotoData(input2byte(input));
} else {
// info.setPhoto(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_person_black_128dp));
}
info.setContactId(id);
info.setDisplayName(displayName);
info.setHasPhoneNumber(phoneCount);

getRawContact(id, info);
Log.i(TAG, "info = " + info.toString());
mContactInfos.add(info);
} while (cur.moveToNext());
cur.close();
}

return mContactInfos;
}

public void getRawContact(int contactId, ContactInfo info) {
//获取联系人RawContacts数据
Cursor rawContactCursor = mContext.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts._ID,
ContactsContract.RawContacts.CONTACT_ID,
ContactsContract.RawContacts.ACCOUNT_TYPE,
ContactsContract.RawContacts.ACCOUNT_NAME},
ContactsContract.RawContacts.CONTACT_ID + "=?",
new String[]{String.valueOf(contactId)},
null);
Log.i(TAG, "rawContactCursor.getCount() = " + rawContactCursor.getCount() + "contactId = " + contactId);
if (rawContactCursor.moveToFirst()) {
do {
// 遍历所有电话号码
int _id = rawContactCursor.getInt(rawContactCursor.getColumnIndex(ContactsContract.RawContacts._ID));
int contact_id = rawContactCursor.getInt(rawContactCursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID));
String account_type = rawContactCursor.getString(rawContactCursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE));
String account_name = rawContactCursor.getString(rawContactCursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME));
ContactInfo.RawContactInfo rawContactInfo = new ContactInfo.RawContactInfo();
rawContactInfo.contactId = contact_id;
rawContactInfo.rawContactId = _id;
rawContactInfo.accountType = account_type;
rawContactInfo.accountName = account_name;
getContactData(_id, rawContactInfo);
info.addRawContact(rawContactInfo);
} while (rawContactCursor.moveToNext());
// 设置联系人电话信息
}
}

/**
* 获取联系人信息,并把数据转换成本地联系人对象
*
* @return
* @throws JSONException
*/
public void getContactData(int rawContactId, ContactInfo.RawContactInfo info) {
String mimetype = "";
String type = "";
int oldrid = -1;
int contactId = -1;

String project1[] = new String[]{
"mimetype", "raw_contact_id", "data1", "data2", "data3", "data4", "data5", "data6", "data7",
"data8", "data9", "data10", "data11", "data12", "data13", "data14", "data15"
};

// 1.查询通讯录所有联系人信息,通过id排序,我们看下android联系人的表就知道,所有的联系人的数据是由RAW_CONTACT_ID来索引开的
// 所以,先获取所有的人的RAW_CONTACT_ID
Cursor cursor = mContext.getContentResolver().query(
Data.CONTENT_URI,
project1,
Data.RAW_CONTACT_ID + "=?",
new String[]{String.valueOf(rawContactId)},
Data.RAW_CONTACT_ID);
int numm = 0;
Log.i(TAG, "datainfo cursor getcount = " + cursor.getCount());
while (cursor.moveToNext()) {
contactId = cursor.getInt(cursor
.getColumnIndex(Data.RAW_CONTACT_ID));
// if (oldrid != contactId) {
// numm++;
// oldrid = contactId;
// }
//Data数据类型
int dataType = ContactDataType.NO_TYPE;

ContactInfo.DataInfo dataInfo = new ContactInfo.DataInfo();
mimetype = cursor.getString(cursor.getColumnIndex(Data.MIMETYPE)); // 取得mimetype类型,扩展的数据都在这个类型里面
type = cursor.getString(cursor.getColumnIndex(Data.DATA2));
dataInfo.rawContactId = contactId;
dataInfo.mimeType = mimetype;
dataInfo.data1 = cursor.getString(cursor.getColumnIndex(Data.DATA1));
dataInfo.data2 = type;
// 1.1,拿到联系人的各种名字
if (StructuredName.CONTENT_ITEM_TYPE.equals(mimetype)) {
dataInfo.data1 = cursor.getString(cursor
.getColumnIndex(StructuredName.DISPLAY_NAME));
dataInfo.data4 = cursor.getString(cursor
.getColumnIndex(StructuredName.PREFIX));
dataInfo.data3 = cursor.getString(cursor
.getColumnIndex(StructuredName.FAMILY_NAME));
dataInfo.data5 = cursor.getString(cursor
.getColumnIndex(StructuredName.MIDDLE_NAME));
dataInfo.data2 = cursor.getString(cursor
.getColumnIndex(StructuredName.GIVEN_NAME));
dataInfo.data6 = cursor.getString(cursor
.getColumnIndex(StructuredName.SUFFIX));
dataInfo.data9 = cursor.getString(cursor
.getColumnIndex(StructuredName.PHONETIC_FAMILY_NAME));
dataInfo.data8 = cursor.getString(cursor
.getColumnIndex(StructuredName.PHONETIC_MIDDLE_NAME));
dataInfo.data7 = cursor.getString(cursor
.getColumnIndex(StructuredName.PHONETIC_GIVEN_NAME));
dataType = ContactDataType.NO_TYPE;
}
// 1.2 获取各种电话信息
if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
dataInfo.data1 = cursor.getString(cursor.getColumnIndex(Phone.NUMBER)).
replaceAll(" ", "").replaceAll("-", "").trim();
dataType = ContactDataType.PHONE_TYPE;
}
//查找Email信息
if (Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
dataInfo.data1 = cursor.getString(cursor.getColumnIndex(Email.ADDRESS));
dataType = ContactDataType.EMAIL_TYPE;
}
// 查找event地址
if (Event.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出时间类型
dataInfo.data1 = cursor.getString(cursor.getColumnIndex(Event.START_DATE));
dataType = ContactDataType.EVENT_TYPE;
}
// 获取即时通讯消息
if (Im.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出即时消息类型
dataInfo.data1 = cursor.getString(cursor.getColumnIndex(Im.DATA));
dataType = ContactDataType.IM_TYPE;
}
// 获取备注信息
if (Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
dataInfo.data1 = cursor.getString(cursor.getColumnIndex(Note.NOTE));
dataType = ContactDataType.NO_TYPE;
}
// 获取昵称信息
if (Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) {
dataInfo.data1 = cursor.getString(cursor.getColumnIndex(Nickname.NAME));
dataType = ContactDataType.NO_TYPE;
}
// 获取组织信息
if (Organization.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出组织类型
dataInfo.data1 = cursor.getString(cursor
.getColumnIndex(Organization.COMPANY));
dataInfo.data4 = cursor.getString(cursor
.getColumnIndex(Organization.TITLE));
dataInfo.data5 = cursor.getString(cursor
.getColumnIndex(Organization.DEPARTMENT));
dataType = ContactDataType.ORGNIZATION_TYPE;
}
// 获取网站信息
if (Website.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出组织类型
dataInfo.data1 = cursor.getString(cursor.getColumnIndex(Website.URL));
dataType = ContactDataType.NO_TYPE;
}
// 查找通讯地址
if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)) { // 取出邮件类型
dataInfo.data4 = cursor.getString(cursor
.getColumnIndex(StructuredPostal.STREET));
dataInfo.data7 = cursor.getString(cursor
.getColumnIndex(StructuredPostal.CITY));
dataInfo.data5 = cursor.getString(cursor
.getColumnIndex(StructuredPostal.POBOX));
dataInfo.data6 = cursor.getString(cursor
.getColumnIndex(StructuredPostal.NEIGHBORHOOD));
dataInfo.data8 = cursor.getString(cursor
.getColumnIndex(StructuredPostal.REGION));
dataInfo.data9 = cursor.getString(cursor
.getColumnIndex(StructuredPostal.POSTCODE));
dataInfo.data10 = cursor.getString(cursor
.getColumnIndex(StructuredPostal.COUNTRY));
dataType = ContactDataType.STRUCTUREDPOSTAL_TYPE;
}
//查找头像数据
if (Photo.CONTENT_ITEM_TYPE.equals(mimetype)) {
dataInfo.data14 = cursor.getString(cursor.getColumnIndex(Photo.PHOTO_FILE_ID));
byte[] buff = cursor.getBlob(cursor.getColumnIndex(Photo.PHOTO));
if (buff != null) {
dataInfo.data15 = Base64.encodeToString(buff, Base64.DEFAULT);
Log.d(TAG, "get photo data15 buff.lenth = " + buff.length);
}
dataType = ContactDataType.NO_TYPE;
}

try {
//type可已转化为int时,通过getLabelNameByType获取typename
dataInfo.typeName = getLabelNameByType(Integer.parseInt(type), dataType);
} catch (NumberFormatException e) {
//type无法转化为int时,直接赋值给typename
dataInfo.typeName = type;
}
// Log.i(TAG, "dataInfo = " + dataInfo.toString());
info.dataInfos.add(dataInfo);
}
cursor.close();
//Log.i(TAG, contactData.toString());
}

/**
* inputStream转换为byte数组
*
* @param inStream
* @return
*/
public static final byte[] input2byte(InputStream inStream) {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[512];
int rc = 0;
try {
while ((rc = inStream.read(buff, 0, 512)) > 0) {
swapStream.write(buff, 0, rc);
}
} catch (IOException e) {
e.printStackTrace();
}
byte[] in2b = swapStream.toByteArray();
Log.d(TAG, "[input2byte] photo.lenth = " + in2b.length);
return in2b;
}

/**
* 根据type值获取标签字符串
*
* @param type
* @return
*/
public static String getLabelNameByType(int type, int dataType) {
if (dataType == ContactDataType.NO_TYPE){
return "";
}
int typeLabelResource = 0;
switch (dataType){
case ContactDataType.PHONE_TYPE:
//通过type获取typelabel字符串
typeLabelResource= Phone.getTypeLabelResource(type);
break;
case ContactDataType.EMAIL_TYPE:
//通过type获取typelabel字符串
typeLabelResource= Email.getTypeLabelResource(type);
break;
case ContactDataType.EVENT_TYPE:
typeLabelResource= Event.getTypeResource(type);
break;
case ContactDataType.IM_TYPE:
typeLabelResource= Im.getTypeLabelResource(type);
break;
case ContactDataType.ORGNIZATION_TYPE:
typeLabelResource= Organization.getTypeLabelResource(type);
break;
case ContactDataType.STRUCTUREDPOSTAL_TYPE:
typeLabelResource= StructuredPostal.getTypeLabelResource(type);
break;
}
if (typeLabelResource == 0){
return "";
}
return mContext.getResources().getString(typeLabelResource);
// Log.i(TAG, "[getLabelNameByType] type = " + type + " string = " + string);
}

/**
* 将二进制数组转换成图片
* @param array
* @return
*/
public static Bitmap byteArray2Bitmap(byte[] array){
if (array == null || array.length <= 0) {
return null;
}

InputStream input = null;
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
input = new ByteArrayInputStream(array);
SoftReference softRef = new SoftReference(BitmapFactory.decodeStream(
input, null, options));
bitmap = (Bitmap) softRef.get();
// if (photoData != null) {
// photoData = null;
// }
try {
if (input != null) {
input.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
// ByteArrayInputStream var6 = new ByteArrayInputStream(photoData);
// Bitmap photo = BitmapFactory.decodeStream(var6);
// Bitmap bitmap = BitmapFactory.decodeByteArray(photoData, 0, photoData.length);
// return bitmap;
}
}

下面代码是对联系人的理解下自行封装的联系人对象,可做参考改动

ContactInfo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import android.graphics.Bitmap;
import android.provider.ContactsContract;
import android.util.Log;

import com.transage.privatespace.utils.BitmapUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
* 联系人信息包装类
* <p>
* Created by yanjie.xu on 2017/8/24.
*/
public class ContactInfo implements Serializable {
private static final String TAG = "ContactInfo";
private int contactId;
private int hasPhoneNumber;
private int photoId;
private int photoFileId;
private String displayName;
private byte[] photoData;
private List<RawContactInfo> rawContactInfos = new ArrayList<>();
private List<Phone> phones = new ArrayList<>();

public static class RawContactInfo implements Serializable {
public int rawContactId;
public int contactId;
public String accountName;
public String accountType;
public List<DataInfo> dataInfos = new ArrayList<>();

@Override
public String toString() {
return "RawContactInfo{" +
"rawContactId=" + rawContactId +
", contactId=" + contactId +
", accountName='" + accountName + '\'' +
", accountType='" + accountType + '\'' +
", dataInfos=" + dataInfos +
'}';
}
}

public static class DataInfo implements Serializable {
public int id;
public String mimeType;
public String typeName;
public int rawContactId;
public String data1;
public String data2;
public String data3;
public String data4;
public String data5;
public String data6;
public String data7;
public String data8;
public String data9;
public String data10;
public String data11;
public String data12;
public String data13;
public String data14;
public String data15;

@Override
public String toString() {
return "DataInfo{" +
"mimeType='" + mimeType + '\'' +
", typeName='" + typeName + '\'' +
", rawContactId=" + rawContactId +
", data1='" + data1 + '\'' +
", data2='" + data2 + '\'' +
", data3='" + data3 + '\'' +
", data4='" + data4 + '\'' +
", data5='" + data5 + '\'' +
", data6='" + data6 + '\'' +
", data7='" + data7 + '\'' +
", data8='" + data8 + '\'' +
", data9='" + data9 + '\'' +
", data10='" + data10 + '\'' +
", data11='" + data11 + '\'' +
", data12='" + data12 + '\'' +
", data13='" + data13 + '\'' +
", data14='" + data14 + '\'' +
", data15='" + data15 + '\'' +
'}';
}
}

public static class Phone implements Serializable {
public String phoneNumber;
public String typeName;

@Override
public String toString() {
return "Phone{" +
"phoneNumber='" + phoneNumber + '\'' +
", typeName='" + typeName + '\'' +
'}';
}
}

public List<RawContactInfo> getRawContactInfos() {
return rawContactInfos;
}

public void setRawContactInfos(List<RawContactInfo> rawContactInfos) {
this.rawContactInfos = rawContactInfos;
}

public int getContactId() {
return contactId;
}

public void setContactId(int contactId) {
this.contactId = contactId;
}

public int getHasPhoneNumber() {
return hasPhoneNumber;
}

public void setHasPhoneNumber(int hasPhoneNumber) {
this.hasPhoneNumber = hasPhoneNumber;
}

public int getPhotoId() {
return photoId;
}

public void setPhotoId(int photoId) {
this.photoId = photoId;
}

public int getPhotoFileId() {
return photoFileId;
}

public void setPhotoFileId(int photoFileId) {
this.photoFileId = photoFileId;
}

public String getDisplayName() {
return displayName;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public byte[] getPhotoData() {
return photoData;
}

public void setPhotoData(byte[] photoData) {
this.photoData = photoData;
}

/**
* 获取该联系人中的所有电话号码
* @return
*/
public List<Phone> getPhones() {
phones.clear();
for (RawContactInfo rawContactInfo : rawContactInfos) {
for (DataInfo dataInfo : rawContactInfo.dataInfos) {
if (ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(dataInfo.mimeType)) {
Phone phone = new Phone();
phone.phoneNumber = dataInfo.data1;
phone.typeName = dataInfo.typeName;
Log.i(TAG, "[getPhones] phone = " + phone.toString());
phones.add(phone);
}
}
}
return phones;
}

/**
* 判断该联系人能否加入私密
* @return
*/
public boolean isPrivateEnable() {
boolean enable = true;
for (RawContactInfo rawContactInfo : rawContactInfos) {
//如果联系人不是sim卡联系人则允许加入私密
if (rawContactInfo.accountName!=null && rawContactInfo.accountName.contains("SIM")){
enable = false;
}
}
return enable;
}

/**
* 获取所有rawContactId
* @return
*/
public int[] getRawContactIds() {
int[] rci = new int[10];
int i = -1;
for (RawContactInfo rawContactInfo : rawContactInfos) {
if (i < 10){
i++;
rci[i] = rawContactInfo.rawContactId;
}
}
return rci;
}

public void addRawContact(RawContactInfo info) {
rawContactInfos.add(info);
}

/**
* 获取联系人头像图片
* @return
*/
public Bitmap getPhoto() {
return BitmapUtils.byteArray2Bitmap(photoData);
}

@Override
public String toString() {
return "ContactInfo{" +
"contactId=" + contactId +
", hasPhoneNumber=" + hasPhoneNumber +
", photoFileId=" + photoFileId +
", photoData=" + photoData +
", displayName='" + displayName + '\'' +
", rawContactInfos=" + rawContactInfos +
", phones=" + phones +
'}';
}
}