-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSIMContacts.java
74 lines (56 loc) · 2.3 KB
/
SIMContacts.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
package com.mtuity.contacts;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import com.mtuity.contacts.model.SimContactBean;
import java.util.ArrayList;
import java.util.List;
/**
* Created by avinash
*/
public class SIMContacts {
private final Context mContxt;
private String defaultString = "";
public SIMContacts(Context context) {
if (context != null) {
mContxt = context;
} else {
throw new NullPointerException("Context should not be null.");
}
}
/**
* @return returns the list of SIM contacts..
*/
public List<SimContactBean> getSIMContacts() {
List<SimContactBean> simContactList = new ArrayList<SimContactBean>();
defaultString = mContxt.getResources().getString(R.string.not_specified);
try {
Uri simUri = Uri.parse("content://icc/adn");
Cursor cursor = mContxt.getContentResolver().query(simUri, null, null, null, null);
Log.i("PhoneContact", "total: " + cursor.getCount());
while (cursor.moveToNext()) {
String name = "name";
String number = "number";
SimContactBean bean = new SimContactBean();
String simContactName = cursor.getString(cursor.getColumnIndex(name));
String simPhoneNumber = cursor.getString(cursor.getColumnIndex(number));
simPhoneNumber.replaceAll("\\D", "");
simPhoneNumber.replaceAll("&", "");
simContactName = simContactName.replace("|", "");
bean.setName(simContactName == null ? defaultString : simContactName);
bean.setNumber(simPhoneNumber == null ? defaultString : simPhoneNumber);
Log.i("PhoneContact", "name: " + simContactName + " phone: " + simPhoneNumber);
simContactList.add(bean);
}
cursor.close();
} catch (SecurityException | IllegalStateException e) {
if (e instanceof SecurityException) {
throw new SecurityException("Permission might not be granted" + e);
} else {
throw new IllegalStateException("IllegalStateException" + e);
}
}
return simContactList;
}
}