|
|
@@ -6,12 +6,9 @@ import android.database.Cursor;
|
|
|
import android.net.Uri;
|
|
|
import android.provider.ContactsContract;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.sheep.gamegroup.model.entity.BaseMessage;
|
|
|
-import com.sheep.gamegroup.model.util.SheepSubscriber;
|
|
|
+import com.sheep.gamegroup.absBase.AbsObserver;
|
|
|
import com.sheep.jiuyan.samllsheep.SheepApp;
|
|
|
-import com.sheep.jiuyan.samllsheep.utils.G;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
@@ -19,7 +16,6 @@ import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import io.reactivex.Observable;
|
|
|
-import io.reactivex.ObservableEmitter;
|
|
|
import io.reactivex.ObservableOnSubscribe;
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
import io.reactivex.schedulers.Schedulers;
|
|
|
@@ -30,15 +26,36 @@ import rx.functions.Action1;
|
|
|
* realicing@sina.com
|
|
|
*/
|
|
|
public class ContactUtil {
|
|
|
+ public static List<Map<String, String>> getAllContact(Context context) {
|
|
|
+ String[] cols = {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
|
|
|
+ Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
|
|
|
+ cols, null, null, null);
|
|
|
+ List<Map<String, String>> list = new ArrayList<>();
|
|
|
+ if (cursor != null) {
|
|
|
+ for (int i = 0; i < cursor.getCount(); i++) {
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ cursor.moveToPosition(i);
|
|
|
+ // 取得联系人名字
|
|
|
+ int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
|
|
|
+ int numberFieldColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
|
|
|
+ String name = cursor.getString(nameFieldColumnIndex);
|
|
|
+ String number = cursor.getString(numberFieldColumnIndex);
|
|
|
+ map.put("name", name);
|
|
|
+ map.put("mobile", number);
|
|
|
+ list.add(map);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 获取通讯录中所有联系人的简单信息
|
|
|
*
|
|
|
* @throws Throwable
|
|
|
*/
|
|
|
- public static List<Map<String, String> > getAllContact(Context context) throws Throwable {
|
|
|
+ public static List<Map<String, String>> getAllContact1(Context context) throws Throwable {
|
|
|
|
|
|
- List<Map<String, String> > list = new ArrayList<>();
|
|
|
+ List<Map<String, String>> list = new ArrayList<>();
|
|
|
Map<String, String> contactIdMap = new HashMap<String, String>();
|
|
|
//获取联系人信息的Uri
|
|
|
Uri uri = ContactsContract.Contacts.CONTENT_URI;
|
|
|
@@ -65,7 +82,7 @@ public class ContactUtil {
|
|
|
null,
|
|
|
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
|
|
|
null, null);
|
|
|
- if(phones != null) {
|
|
|
+ if (phones != null) {
|
|
|
while (phones.moveToNext()) {
|
|
|
String phoneNumber = phones.getString(phones.getColumnIndex(
|
|
|
ContactsContract.CommonDataKinds.Phone.NUMBER));
|
|
|
@@ -95,32 +112,37 @@ public class ContactUtil {
|
|
|
|
|
|
|
|
|
public static void getAllContactList(Action1<String> action1) {
|
|
|
- Observable.create((ObservableOnSubscribe<BaseMessage>) observableEmitter -> {
|
|
|
- BaseMessage baseMessage = new BaseMessage();
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
+ Observable.create((ObservableOnSubscribe<JSONObject>) observableEmitter -> {
|
|
|
try {
|
|
|
+ long time = System.currentTimeMillis();
|
|
|
List<Map<String, String>> list = getAllContact(SheepApp.getInstance());
|
|
|
- baseMessage.setCode(200);
|
|
|
- baseMessage.setData(list);
|
|
|
- baseMessage.setTotal(ListUtil.size(list));
|
|
|
+ result.put("code", 200);
|
|
|
+ result.put("msg", "获取通讯录成功");
|
|
|
+ result.put("takeTime", System.currentTimeMillis() - time);
|
|
|
+ result.put("data", list);
|
|
|
+ result.put("total", ListUtil.size(list));
|
|
|
} catch (Throwable throwable) {
|
|
|
throwable.printStackTrace();
|
|
|
- baseMessage.setMsg(throwable.getMessage());
|
|
|
- baseMessage.setCode(500);
|
|
|
+ result.put("code", 500);
|
|
|
+ result.put("msg", throwable.getMessage());
|
|
|
}
|
|
|
- observableEmitter.onNext(baseMessage);
|
|
|
+ observableEmitter.onNext(result);
|
|
|
})
|
|
|
- .subscribeOn(Schedulers.io())
|
|
|
- .observeOn(AndroidSchedulers.mainThread())
|
|
|
- .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
|
|
|
- @Override
|
|
|
- public void onNext(BaseMessage baseMessage) {
|
|
|
- action1.call(JSON.toJSONString(baseMessage));
|
|
|
- }
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
+ .subscribe(new AbsObserver<JSONObject>() {
|
|
|
+ @Override
|
|
|
+ public void onNext(JSONObject result) {
|
|
|
+ action1.call(result.toJSONString());
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void onError(BaseMessage baseMessage) {
|
|
|
- action1.call(JSON.toJSONString(baseMessage));
|
|
|
- }
|
|
|
- });
|
|
|
+ @Override
|
|
|
+ public void onError(Throwable throwable) {
|
|
|
+ result.put("code", 600);
|
|
|
+ result.put("msg", throwable.getMessage());
|
|
|
+ action1.call(result.toJSONString());
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
}
|