123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- package com.sikey.skcontact.manager;
- import android.annotation.SuppressLint;
- import android.content.ContentResolver;
- import android.content.Context;
- import android.database.Cursor;
- import android.net.Uri;
- import android.provider.CallLog;
- import android.util.Log;
- import com.sikey.skcontact.ContactApplication;
- import com.sikey.skcontact.model.ContactBean;
- import com.sikey.skcontact.model.RecentBean;
- import com.sikey.skcontact.utils.Macros;
- import com.sikey.skcontact.utils.MetaData;
- import com.sikey.skcontact.utils.ToolsUtils;
- import java.util.ArrayList;
- import java.util.List;
- public class DatabaseUtils {
- public static String TAG = "losion / skcontact :" + "DatabaseUtils";
- public DatabaseUtils() {}
- @SuppressLint("Range")
- public static ArrayList<ContactBean> queryContacts(Context context) {
- if (Macros.DEBUG) {
- return testContactList();
- } else {
- ArrayList<ContactBean> arrayList = new ArrayList<>();
- ContentResolver resolver = context.getContentResolver();
- Uri uri = Uri.parse(MetaData.DB_CONTACTS);
- Cursor cursor = resolver.query(uri, null, null, null, null);
- if (cursor == null) {
- Log.d(TAG, "queryContacts: cursor == null");
- return arrayList;
- }
- if (!cursor.moveToFirst()) {
- Log.d(TAG, "queryContacts: cursor.moveToFirst() == false");
- cursor.close();
- return arrayList;
- }
- Log.d(TAG, "queryContacts: db ok");
- do {
- ContactBean bean = new ContactBean();
- bean.id = cursor.getString(cursor.getColumnIndex("id"));
- bean.name = cursor.getString(cursor.getColumnIndex("name"));
- bean.profile = cursor.getString(cursor.getColumnIndex("profile"));
- bean.profilePath = cursor.getString(cursor.getColumnIndex("profilePath"));
- bean.phoneNumber = cursor.getString(cursor.getColumnIndex("phoneNumber"));
- bean.countryPN = cursor.getString(cursor.getColumnIndex("countryPN"));
- bean.type = ToolsUtils.parseInt(cursor.getString(cursor.getColumnIndex("type")));
- bean.rate = ToolsUtils.parseInt(cursor.getString(cursor.getColumnIndex("rate")));
- bean.unRead = ToolsUtils.parseInt(cursor.getString(cursor.getColumnIndex("unRead")));
- arrayList.add(bean);
- } while (cursor.moveToNext());
- cursor.close();
- Log.d(TAG, "queryContacts: " + arrayList.size());
- return arrayList;
- }
- }
- @SuppressLint("Range")
- public static ArrayList<RecentBean> queryRecent(Context context, int offset) {
- if (Macros.DEBUG) {
- return testRecentList();
- } else {
- StringBuilder sortOrder = new StringBuilder();
- sortOrder.append(CallLog.Calls.DATE).append(" desc ").append(" limit ").append(20).append(" Offset ").append(offset);
- ArrayList<RecentBean> arrayList = new ArrayList<>();
- ContentResolver resolver = context.getContentResolver();
- Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, null, null, null, sortOrder.toString());
- if (cursor == null) {
- Log.d(TAG, "queryRecent: cursor == null");
- return arrayList;
- }
- if (!cursor.moveToFirst()) {
- Log.d(TAG, "queryRecent: cursor.moveToFirst false");
- cursor.close();
- return arrayList;
- }
- Log.d(TAG, "queryRecent: db ok");
- do {
- RecentBean bean = new RecentBean();
- bean._id = cursor.getInt(cursor.getColumnIndex("_id"));
- bean.type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));//通话类型,1 来电 .INCOMING_TYPE;2 已拨 .OUTGOING_;3 未接 .MISSED_
- bean.caller = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
- bean.start = (int)(cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE)) / 1000);
- bean.duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));
- arrayList.add(bean);
- } while (cursor.moveToNext());
- cursor.close();
- Log.d(TAG, "queryRecent: " + arrayList.size());
- return arrayList;
- }
- }
- public static ArrayList<ContactBean> testContactList() {
- ArrayList<ContactBean> arrayList = new ArrayList<>();
- for (int i = 0; i < 8; i++) {
- ContactBean bean = new ContactBean();
- bean.id = "2000";
- bean.name = "abcdefghijk"+ i;
- bean.phoneNumber = "1111";
- arrayList.add(bean);
- }
- return arrayList;
- }
- public static ArrayList<RecentBean> testRecentList() {
- ArrayList<RecentBean> recentList = new ArrayList<>();
- for (int i = 1; i < 20; i++) {
- RecentBean model = new RecentBean();
- model.caller = "1111";
- model.type = i;
- model.start = 1669310077; //ToolsUtils.getCurrentTimeSecond(); //1467442671;
- recentList.add(model);
- }
- return recentList;
- }
- }
|