DatabaseUtils.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.sikey.skcontact.manager;
  2. import android.annotation.SuppressLint;
  3. import android.content.ContentResolver;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.net.Uri;
  7. import android.provider.CallLog;
  8. import android.util.Log;
  9. import com.sikey.skcontact.ContactApplication;
  10. import com.sikey.skcontact.model.ContactBean;
  11. import com.sikey.skcontact.model.RecentBean;
  12. import com.sikey.skcontact.utils.Macros;
  13. import com.sikey.skcontact.utils.MetaData;
  14. import com.sikey.skcontact.utils.ToolsUtils;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. public class DatabaseUtils {
  18. public static String TAG = "losion / skcontact :" + "DatabaseUtils";
  19. public DatabaseUtils() {}
  20. @SuppressLint("Range")
  21. public static ArrayList<ContactBean> queryContacts(Context context) {
  22. if (Macros.DEBUG) {
  23. return testContactList();
  24. } else {
  25. ArrayList<ContactBean> arrayList = new ArrayList<>();
  26. ContentResolver resolver = context.getContentResolver();
  27. Uri uri = Uri.parse(MetaData.DB_CONTACTS);
  28. Cursor cursor = resolver.query(uri, null, null, null, null);
  29. if (cursor == null) {
  30. Log.d(TAG, "queryContacts: cursor == null");
  31. return arrayList;
  32. }
  33. if (!cursor.moveToFirst()) {
  34. Log.d(TAG, "queryContacts: cursor.moveToFirst() == false");
  35. cursor.close();
  36. return arrayList;
  37. }
  38. Log.d(TAG, "queryContacts: db ok");
  39. do {
  40. ContactBean bean = new ContactBean();
  41. bean.id = cursor.getString(cursor.getColumnIndex("id"));
  42. bean.name = cursor.getString(cursor.getColumnIndex("name"));
  43. bean.profile = cursor.getString(cursor.getColumnIndex("profile"));
  44. bean.profilePath = cursor.getString(cursor.getColumnIndex("profilePath"));
  45. bean.phoneNumber = cursor.getString(cursor.getColumnIndex("phoneNumber"));
  46. bean.countryPN = cursor.getString(cursor.getColumnIndex("countryPN"));
  47. bean.type = ToolsUtils.parseInt(cursor.getString(cursor.getColumnIndex("type")));
  48. bean.rate = ToolsUtils.parseInt(cursor.getString(cursor.getColumnIndex("rate")));
  49. bean.unRead = ToolsUtils.parseInt(cursor.getString(cursor.getColumnIndex("unRead")));
  50. arrayList.add(bean);
  51. } while (cursor.moveToNext());
  52. cursor.close();
  53. Log.d(TAG, "queryContacts: " + arrayList.size());
  54. return arrayList;
  55. }
  56. }
  57. @SuppressLint("Range")
  58. public static ArrayList<RecentBean> queryRecent(Context context, int offset) {
  59. if (Macros.DEBUG) {
  60. return testRecentList();
  61. } else {
  62. StringBuilder sortOrder = new StringBuilder();
  63. sortOrder.append(CallLog.Calls.DATE).append(" desc ").append(" limit ").append(20).append(" Offset ").append(offset);
  64. ArrayList<RecentBean> arrayList = new ArrayList<>();
  65. ContentResolver resolver = context.getContentResolver();
  66. Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, null, null, null, sortOrder.toString());
  67. if (cursor == null) {
  68. Log.d(TAG, "queryRecent: cursor == null");
  69. return arrayList;
  70. }
  71. if (!cursor.moveToFirst()) {
  72. Log.d(TAG, "queryRecent: cursor.moveToFirst false");
  73. cursor.close();
  74. return arrayList;
  75. }
  76. Log.d(TAG, "queryRecent: db ok");
  77. do {
  78. RecentBean bean = new RecentBean();
  79. bean._id = cursor.getInt(cursor.getColumnIndex("_id"));
  80. bean.type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));//通话类型,1 来电 .INCOMING_TYPE;2 已拨 .OUTGOING_;3 未接 .MISSED_
  81. bean.caller = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
  82. bean.start = (int)(cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE)) / 1000);
  83. bean.duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));
  84. arrayList.add(bean);
  85. } while (cursor.moveToNext());
  86. cursor.close();
  87. Log.d(TAG, "queryRecent: " + arrayList.size());
  88. return arrayList;
  89. }
  90. }
  91. public static ArrayList<ContactBean> testContactList() {
  92. ArrayList<ContactBean> arrayList = new ArrayList<>();
  93. for (int i = 0; i < 8; i++) {
  94. ContactBean bean = new ContactBean();
  95. bean.id = "2000";
  96. bean.name = "abcdefghijk"+ i;
  97. bean.phoneNumber = "1111";
  98. arrayList.add(bean);
  99. }
  100. return arrayList;
  101. }
  102. public static ArrayList<RecentBean> testRecentList() {
  103. ArrayList<RecentBean> recentList = new ArrayList<>();
  104. for (int i = 1; i < 20; i++) {
  105. RecentBean model = new RecentBean();
  106. model.caller = "1111";
  107. model.type = i;
  108. model.start = 1669310077; //ToolsUtils.getCurrentTimeSecond(); //1467442671;
  109. recentList.add(model);
  110. }
  111. return recentList;
  112. }
  113. }