/app/src/main/java/com/centersoft/db/DBManager.java

https://github.com/githuanl/socket.io-android-client · Java · 403 lines · 261 code · 95 blank · 47 comment · 28 complexity · 351a8a32651dfeb2a5d1e0406cd0bb0f MD5 · raw file

  1. package com.centersoft.db;
  2. import android.content.ContentValues;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.text.TextUtils;
  6. import com.alibaba.fastjson.JSON;
  7. import com.centersoft.base.ChatApplication;
  8. import com.centersoft.db.dao.ConversationDao;
  9. import com.centersoft.db.dao.VFMessageDao;
  10. import com.centersoft.entity.Bodies;
  11. import com.centersoft.entity.VFMessage;
  12. import com.centersoft.enums.Chat_type;
  13. import com.centersoft.util.Constant;
  14. import com.centersoft.util.MyLog;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. public class DBManager {
  18. static private DBManager dbMgr = new DBManager();
  19. private DbOpenHelper dbHelper;
  20. private DBManager() {
  21. dbHelper = DbOpenHelper.getInstance(ChatApplication.getApplication());
  22. }
  23. public static synchronized DBManager getInstance() {
  24. if (dbMgr == null) {
  25. dbMgr = new DBManager();
  26. }
  27. return dbMgr;
  28. }
  29. /**
  30. * 保存message
  31. *
  32. * @param message
  33. * @return 返回这条messaged在db中的id
  34. */
  35. public synchronized Integer saveMessage(VFMessage message) {
  36. SQLiteDatabase db = dbHelper.getWritableDatabase();
  37. int id = -1;
  38. if (db.isOpen()) {
  39. ContentValues values = new ContentValues();
  40. values.put(VFMessageDao.COLUMN_NAME_GROUP_ID, message.getGroup_id());
  41. values.put(VFMessageDao.COLUMN_NAME_GROUP_NAME, message.getGroup_name());
  42. values.put(VFMessageDao.COLUMN_NAME_MESSAGE_ID, message.getMsg_id());
  43. values.put(VFMessageDao.COLUMN_NAME_TIMESTAMP, message.getTimestamp());
  44. values.put(VFMessageDao.COLUMN_NAME_FROM_USER, message.getFrom_user());
  45. values.put(VFMessageDao.COLUMN_NAME_TO_USER, message.getTo_user());
  46. values.put(VFMessageDao.COLUMN_NAME_STATUS, message.getStatus());
  47. values.put(VFMessageDao.COLUMN_NAME_CHAT_TYPE, message.getChat_type().toString());
  48. values.put(VFMessageDao.COLUMN_NAME_EXT, message.getExt());
  49. values.put(VFMessageDao.COLUMN_NAME_BODIES, JSON.toJSONString(message.getBodies()));
  50. db.insert(VFMessageDao.TABLE_NAME, null, values);
  51. Cursor cursor = db.rawQuery("select last_insert_rowid() from " + VFMessageDao.TABLE_NAME, null);
  52. if (cursor.moveToFirst()) {
  53. id = cursor.getInt(0);
  54. }
  55. MyLog.i("save-message", "保存成功 " + id);
  56. cursor.close();
  57. }
  58. return id;
  59. }
  60. /**
  61. * 保存 List message
  62. *
  63. * @return 返回这条messaged在db中的id
  64. */
  65. public synchronized Integer saveListMessage(List<VFMessage> messages) {
  66. SQLiteDatabase db = dbHelper.getWritableDatabase();
  67. int id = -1;
  68. if (db.isOpen()) {
  69. for (VFMessage message : messages) {
  70. ContentValues values = new ContentValues();
  71. values.put(VFMessageDao.COLUMN_NAME_GROUP_ID, message.getGroup_id());
  72. values.put(VFMessageDao.COLUMN_NAME_GROUP_NAME, message.getGroup_name());
  73. values.put(VFMessageDao.COLUMN_NAME_MESSAGE_ID, message.getMsg_id());
  74. values.put(VFMessageDao.COLUMN_NAME_TIMESTAMP, message.getTimestamp());
  75. values.put(VFMessageDao.COLUMN_NAME_FROM_USER, message.getFrom_user());
  76. values.put(VFMessageDao.COLUMN_NAME_TO_USER, message.getTo_user());
  77. values.put(VFMessageDao.COLUMN_NAME_STATUS, message.getStatus());
  78. values.put(VFMessageDao.COLUMN_NAME_CHAT_TYPE, message.getChat_type().toString());
  79. values.put(VFMessageDao.COLUMN_NAME_EXT, message.getExt());
  80. values.put(VFMessageDao.COLUMN_NAME_BODIES, JSON.toJSONString(message.getBodies()));
  81. db.insert(VFMessageDao.TABLE_NAME, null, values);
  82. }
  83. Cursor cursor = db.rawQuery("select last_insert_rowid() from " + VFMessageDao.TABLE_NAME, null);
  84. if (cursor.moveToFirst()) {
  85. id = cursor.getInt(0);
  86. }
  87. cursor.close();
  88. }
  89. return id;
  90. }
  91. /**
  92. * 更新message
  93. *
  94. * @param msgId
  95. * @param values
  96. */
  97. synchronized public void updateMessageWithMsgId(String msgId, ContentValues values) {
  98. SQLiteDatabase db = dbHelper.getWritableDatabase();
  99. if (db.isOpen()) {
  100. db.update(VFMessageDao.TABLE_NAME, values, VFMessageDao.COLUMN_NAME_MESSAGE_ID + " = ?", new String[]{String.valueOf(msgId)});
  101. }
  102. }
  103. /**
  104. * 获取当前 对应的聊天人的 历史聊天记录
  105. *
  106. * @return
  107. */
  108. synchronized public List<VFMessage> getMessageListWithToUser(String fromUser, String toUser, int offset, int limit) {
  109. SQLiteDatabase db = dbHelper.getReadableDatabase();
  110. List<VFMessage> msgs = new ArrayList<VFMessage>();
  111. if (db.isOpen()) {
  112. Cursor cursor = db.rawQuery("select * from (select * from " + VFMessageDao.TABLE_NAME
  113. + " where from_user in (?,?) and to_user in(?,?) order by id DESC limit ?,? ) order by id asc", new String[]{
  114. fromUser, toUser, fromUser, toUser, offset + "", limit + ""
  115. });
  116. while (cursor.moveToNext()) {
  117. int id = cursor.getInt(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_ID));
  118. String groupid = cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_GROUP_ID));
  119. String groupname = cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_GROUP_NAME));
  120. String msg_id = cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_MESSAGE_ID));
  121. long timestamp = cursor.getLong(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_TIMESTAMP));
  122. String from_user = cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_FROM_USER));
  123. String to_user = cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_TO_USER));
  124. String status = cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_STATUS));
  125. Chat_type chat_type = Chat_type.valueOf(cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_CHAT_TYPE)));
  126. String ext = cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_EXT));
  127. Bodies bodies = JSON.parseObject(cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_BODIES)), Bodies.class);
  128. VFMessage msg = new VFMessage(from_user, to_user, chat_type, ext, bodies);
  129. msg.setId(id);
  130. msg.setGroup_id(groupid);
  131. msg.setGroup_name(groupname);
  132. msg.setTimestamp(timestamp);
  133. msg.setStatus(status);
  134. msg.setMsg_id(msg_id);
  135. msgs.add(msg);
  136. }
  137. cursor.close();
  138. }
  139. return msgs;
  140. }
  141. /**
  142. * 获取messges
  143. *
  144. * @return
  145. */
  146. synchronized public List<VFMessage> getAllMessagesList() {
  147. SQLiteDatabase db = dbHelper.getReadableDatabase();
  148. List<VFMessage> msgs = new ArrayList<VFMessage>();
  149. if (db.isOpen()) {
  150. Cursor cursor = db.rawQuery("select * from " + VFMessageDao.TABLE_NAME + " order by id asc", null);
  151. while (cursor.moveToNext()) {
  152. int id = cursor.getInt(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_ID));
  153. String groupid = cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_GROUP_ID));
  154. String groupname = cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_GROUP_NAME));
  155. String msg_id = cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_MESSAGE_ID));
  156. long timestamp = cursor.getLong(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_TIMESTAMP));
  157. String from_user = cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_FROM_USER));
  158. String to_user = cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_TO_USER));
  159. String status = cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_STATUS));
  160. Chat_type chat_type = Chat_type.valueOf(cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_CHAT_TYPE)));
  161. String ext = cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_EXT));
  162. Bodies bodies = JSON.parseObject(cursor.getString(cursor.getColumnIndex(VFMessageDao.COLUMN_NAME_BODIES)), Bodies.class);
  163. VFMessage msg = new VFMessage(from_user, to_user, chat_type, ext, bodies);
  164. msg.setId(id);
  165. msg.setGroup_id(groupid);
  166. msg.setGroup_name(groupname);
  167. msg.setTimestamp(timestamp);
  168. msg.setStatus(status);
  169. msg.setMsg_id(msg_id);
  170. msgs.add(msg);
  171. }
  172. cursor.close();
  173. }
  174. return msgs;
  175. }
  176. /**
  177. * 根据msg 删除 对应的消息数据
  178. */
  179. public synchronized void deleteMessageWithMsgId(String msgId) {
  180. SQLiteDatabase db = dbHelper.getWritableDatabase();
  181. if (db.isOpen()) {
  182. db.delete(VFMessageDao.TABLE_NAME, VFMessageDao.COLUMN_NAME_MESSAGE_ID + " = ?", new String[]{msgId});
  183. }
  184. }
  185. /**** ------------------------- 会话列表 start ---------------------------------****/
  186. /**
  187. * 保存历史会话
  188. */
  189. public synchronized int saveOrUpdateConversation(VFMessage message) {
  190. SQLiteDatabase db = dbHelper.getWritableDatabase();
  191. int num = 0;
  192. if (db.isOpen()) {
  193. Cursor c = db.rawQuery("SELECT from_user, msg_id,unreadnum FROM " + ConversationDao.TABLE_NAME
  194. + " WHERE " + ConversationDao.COLUMN_NAME_FROM_USER + " in (?,?) and " + ConversationDao.COLUMN_NAME_TO_USER
  195. + " in (?,?)", new String[]{message.getFrom_user(), message.getTo_user(), message.getFrom_user(), message.getTo_user()});
  196. ContentValues values = new ContentValues();
  197. values.put(ConversationDao.COLUMN_NAME_GROUP_ID, message.getGroup_id());
  198. values.put(ConversationDao.COLUMN_NAME_GROUP_NAME, message.getGroup_name());
  199. values.put(ConversationDao.COLUMN_NAME_MESSAGE_ID, message.getMsg_id());
  200. values.put(ConversationDao.COLUMN_NAME_TIMESTAMP, message.getTimestamp());
  201. values.put(ConversationDao.COLUMN_NAME_FROM_USER, message.getFrom_user());
  202. values.put(ConversationDao.COLUMN_NAME_TO_USER, message.getTo_user());
  203. values.put(ConversationDao.COLUMN_NAME_STATUS, message.getStatus());
  204. values.put(ConversationDao.COLUMN_NAME_CHAT_TYPE, message.getChat_type().toString());
  205. values.put(ConversationDao.COLUMN_NAME_EXT, message.getExt());
  206. values.put(ConversationDao.COLUMN_NAME_BODIES, JSON.toJSONString(message.getBodies()));
  207. if (c.moveToFirst()) {
  208. String msg_id = c.getString(c.getColumnIndex(ConversationDao.COLUMN_NAME_MESSAGE_ID));
  209. if ((TextUtils.isEmpty(Constant.chatToUser) || !Constant.chatToUser.equals(message.getFrom_user()))
  210. && !message.getFrom_user().equals(Constant.Login_Name)) { //不是聊天界面的时候添加未读数
  211. int unreadnum = c.getInt(c.getColumnIndex(ConversationDao.COLUMN_NAME_UNREAD_NUM));
  212. num = unreadnum + 1;
  213. values.put(ConversationDao.COLUMN_NAME_UNREAD_NUM, num);
  214. }
  215. db.update(ConversationDao.TABLE_NAME, values, "msg_id = ?", new String[]{msg_id});
  216. } else {
  217. values.put(ConversationDao.COLUMN_NAME_UNREAD_NUM, 1);
  218. db.insert(ConversationDao.TABLE_NAME, null, values);
  219. }
  220. c.close();
  221. }
  222. return num;
  223. }
  224. /**
  225. * 设置历史会话 未读数量为0
  226. */
  227. public synchronized void updateConversationUnReadNum(String fromUser, String toUser) {
  228. SQLiteDatabase db = dbHelper.getWritableDatabase();
  229. if (db.isOpen()) {
  230. Cursor cursor = db.rawQuery("select msg_id from " + ConversationDao.TABLE_NAME + " where from_user in (?,?) and to_user in(?,?) order by id asc", new String[]{
  231. fromUser, toUser, fromUser, toUser
  232. });
  233. if (cursor.moveToFirst()) {
  234. String msg_id = cursor.getString(cursor.getColumnIndex(ConversationDao.COLUMN_NAME_MESSAGE_ID));
  235. ContentValues values = new ContentValues();
  236. values.put(ConversationDao.COLUMN_NAME_UNREAD_NUM, 0);
  237. db.update(ConversationDao.TABLE_NAME, values, "msg_id = ?", new String[]{msg_id});
  238. }
  239. cursor.close();
  240. }
  241. }
  242. /**
  243. * 获取所有会话的未读数的总条数
  244. */
  245. public synchronized int getAllConversationUnreadNum() {
  246. SQLiteDatabase db = dbHelper.getWritableDatabase();
  247. int num = 0;
  248. if (db.isOpen()) {
  249. Cursor cursor = db.rawQuery("select SUM(unreadnum) as countnum from " + ConversationDao.TABLE_NAME, null);
  250. if (cursor.moveToFirst()) {
  251. num = cursor.getInt(cursor.getColumnIndex("countnum"));
  252. }
  253. cursor.close();
  254. }
  255. return num;
  256. }
  257. /**
  258. * 根据msg id 删除 对应的历史会话
  259. */
  260. public synchronized void deleteConversationWithMsgId(String msgId) {
  261. SQLiteDatabase db = dbHelper.getWritableDatabase();
  262. if (db.isOpen()) {
  263. db.delete(ConversationDao.TABLE_NAME, ConversationDao.COLUMN_NAME_MESSAGE_ID + " = ?", new String[]{msgId});
  264. }
  265. }
  266. /**
  267. * 查询所有的历史会话
  268. */
  269. public synchronized List<VFMessage> getAllConversation() {
  270. SQLiteDatabase db = dbHelper.getReadableDatabase();
  271. List<VFMessage> msgs = new ArrayList<VFMessage>();
  272. if (db.isOpen()) {
  273. Cursor cursor = db.rawQuery("select * from " + ConversationDao.TABLE_NAME + " order by " + ConversationDao.COLUMN_NAME_TIMESTAMP + " desc", null);
  274. while (cursor.moveToNext()) {
  275. int id = cursor.getInt(cursor.getColumnIndex(ConversationDao.COLUMN_NAME_ID));
  276. String groupid = cursor.getString(cursor.getColumnIndex(ConversationDao.COLUMN_NAME_GROUP_ID));
  277. String groupname = cursor.getString(cursor.getColumnIndex(ConversationDao.COLUMN_NAME_GROUP_NAME));
  278. String msg_id = cursor.getString(cursor.getColumnIndex(ConversationDao.COLUMN_NAME_MESSAGE_ID));
  279. long timestamp = cursor.getLong(cursor.getColumnIndex(ConversationDao.COLUMN_NAME_TIMESTAMP));
  280. int unreadnum = cursor.getInt(cursor.getColumnIndex(ConversationDao.COLUMN_NAME_UNREAD_NUM));
  281. String from_user = cursor.getString(cursor.getColumnIndex(ConversationDao.COLUMN_NAME_FROM_USER));
  282. String to_user = cursor.getString(cursor.getColumnIndex(ConversationDao.COLUMN_NAME_TO_USER));
  283. String status = cursor.getString(cursor.getColumnIndex(ConversationDao.COLUMN_NAME_STATUS));
  284. Chat_type chat_type = Chat_type.valueOf(cursor.getString(cursor.getColumnIndex(ConversationDao.COLUMN_NAME_CHAT_TYPE)));
  285. String ext = cursor.getString(cursor.getColumnIndex(ConversationDao.COLUMN_NAME_EXT));
  286. Bodies bodies = JSON.parseObject(cursor.getString(cursor.getColumnIndex(ConversationDao.COLUMN_NAME_BODIES)), Bodies.class);
  287. VFMessage msg = new VFMessage(from_user, to_user, chat_type, ext, bodies);
  288. msg.setId(id);
  289. msg.setGroup_id(groupid);
  290. msg.setGroup_name(groupname);
  291. msg.setTimestamp(timestamp);
  292. msg.setStatus(status);
  293. msg.setMsg_id(msg_id);
  294. msg.setUnreadnum(unreadnum);
  295. msgs.add(msg);
  296. }
  297. cursor.close();
  298. }
  299. return msgs;
  300. }
  301. /**** ------------------------- 会话列表 end ---------------------------------****/
  302. synchronized public void closeDB() {
  303. if (dbHelper != null) {
  304. dbHelper.closeDB();
  305. }
  306. dbMgr = null;
  307. }
  308. }