/** 更新消息的已读状态 */ public void updateMessageReadState(MessageInfo info) { SQLiteDatabase dbSlaver = helper.getReadableDatabase(); ContentValues values = new ContentValues(); values.put("isRead", info.getIsRead()); String whereClause = "messageid=?"; String[] whereArgs = {info.getMessageId()}; int row = dbSlaver.update("t_message", values, whereClause, whereArgs); }
/** 修改录音文件的路径 */ public boolean updateAudioFilePathByMessageId(MessageInfo info) { SQLiteDatabase dbMaster = null; try { dbMaster = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("audioFilePath", info.getAudioFilePath()); String whereClause = "messageid=?"; String[] whereArgs = {info.getMessageId()}; int row = dbMaster.update("t_message", values, whereClause, whereArgs); if (row > 0) { return true; } } catch (Exception e) { } return false; }
/** * 删除一条消息 * * @param msg * @return Boolean */ public Boolean delete(MessageInfo msg) { Boolean boolRtn = false; if (null == msg) { return boolRtn; } SQLiteDatabase dbMaster = null; try { dbMaster = helper.getWritableDatabase(); dbMaster.beginTransaction(); String msgId = msg.getMessageId(); dbMaster.delete("t_message", "" + "msgId" + " == ?", new String[] {msgId}); dbMaster.setTransactionSuccessful(); boolRtn = true; } catch (SQLException e) { LogUtil.info(e.toString()); } finally { if (null != dbMaster) { dbMaster.endTransaction(); } // dbMaster.close(); } return boolRtn; }
/** * 新增一条消息 * * @param msg 消息体 * @return msgId 消息存储的唯一ID */ public void saveMsg(MessageInfo msg) { SQLiteDatabase dbMaster = null; try { dbMaster = helper.getWritableDatabase(); dbMaster.beginTransaction(); dbMaster.execSQL( IMDbHelper.INSERT_MESSAGE_SQL, new Object[] { msg.getMessageId(), msg.getFromuserId(), msg.getTouserId(), msg.getIsRequireEncryption(), msg.getIsEncryptedOnServer(), msg.getIsRead(), msg.getTimesTamp(), msg.getIsReadacked(), msg.getIsDeliveredacked(), msg.getContentType(), msg.getContent(), msg.getConversationId(), msg.getSenderName(), msg.getExt(), msg.getDeliveryState(), msg.getIsAnonymous(), msg.getMessageType(), msg.getDuration(), msg.getPortraitImg(), msg.getAudioFilePath() }); dbMaster.setTransactionSuccessful(); } catch (SQLException e) { LogUtil.info(e.toString()); } finally { if (null != dbMaster) { dbMaster.endTransaction(); } // dbMaster.close(); } }
/** 查询最后一条聊天记录 */ public MessageInfo queryMessageInfoByFromIdAndToId(String fromId, String toId) { MessageInfo msg = null; Cursor c = null; SQLiteDatabase dbSlaver = null; try { dbSlaver = helper.getReadableDatabase(); String querySql = "select * from t_message where (fromuserid like ? and touserid like ?) or (touserid like ? and fromuserid like ?) order by mid desc limit 1"; c = dbSlaver.rawQuery( querySql, new String[] {fromId + "%", toId + "%", fromId + "%", toId + "%"}); if (c != null && c.moveToFirst()) { msg = new MessageInfo(); msg.setMid(c.getInt(c.getColumnIndex("mid"))); msg.setMessageId(c.getString(c.getColumnIndex("messageid"))); msg.setFromuserId(c.getString(c.getColumnIndex("fromuserid"))); msg.setTouserId(c.getString(c.getColumnIndex("touserid"))); msg.setIsRequireEncryption(c.getInt(c.getColumnIndex("isrequireencryption"))); msg.setIsEncryptedOnServer(c.getInt(c.getColumnIndex("isencryptedonserver"))); msg.setIsRead(c.getInt(c.getColumnIndex("isRead"))); msg.setTimesTamp(c.getString(c.getColumnIndex("timestamp"))); msg.setIsReadacked(c.getInt(c.getColumnIndex("isreadacked"))); msg.setIsDeliveredacked(c.getInt(c.getColumnIndex("isdeliveredacked"))); msg.setContentType(c.getInt(c.getColumnIndex("contenttype"))); msg.setContent(c.getString(c.getColumnIndex("content"))); msg.setConversationId(c.getString(c.getColumnIndex("conversationid"))); msg.setSenderName(c.getString(c.getColumnIndex("sendername"))); msg.setExt(c.getString(c.getColumnIndex("ext"))); msg.setDeliveryState(c.getInt(c.getColumnIndex("deliverystate"))); msg.setIsAnonymous(c.getInt(c.getColumnIndex("isanonymous"))); msg.setMessageType(c.getInt(c.getColumnIndex("messagetype"))); msg.setPortraitImg(c.getString(c.getColumnIndex("portraitimg"))); } } catch (SQLException e) { LogUtil.info(e.toString()); } finally { if (null != c) { c.close(); } } return msg; }
/** 根据conversationId查询消息记录 */ public List<MessageInfo> queryMessageInfosByFromIdAndToId( String fromId, String toId, int num, int startIndex) { MessageInfo msg = null; List<MessageInfo> msgList = new ArrayList<MessageInfo>(); Cursor c = null; SQLiteDatabase dbSlaver = null; try { dbSlaver = helper.getReadableDatabase(); String querySql = "select * from t_message" + " where (fromuserid like ? and touserid like ?) or (touserid like ? and fromuserid like ?) order by mid desc"; if (num > 0 && startIndex >= 0) { querySql = querySql + "limit " + num + " offset " + startIndex; } c = dbSlaver.rawQuery( querySql, new String[] {fromId + "%", toId + "%", fromId + "%", toId + "%"}); if (c != null && c.getCount() > 0) { while (c.moveToNext()) { msg = new MessageInfo(); msg.setMid(c.getInt(c.getColumnIndex("mid"))); msg.setMessageId(c.getString(c.getColumnIndex(IMDbHelper.MESSAGE_ID))); msg.setFromuserId(c.getString(c.getColumnIndex(IMDbHelper.FROM_USER_ID))); msg.setTouserId(c.getString(c.getColumnIndex(IMDbHelper.TO_USER_ID))); msg.setIsRequireEncryption(c.getInt(c.getColumnIndex(IMDbHelper.IS_REQUIRE_ENCRYPTION))); msg.setIsEncryptedOnServer(c.getInt(c.getColumnIndex(IMDbHelper.IS_ENCRYPTED_ON_SERVER))); msg.setIsRead(c.getInt(c.getColumnIndex(IMDbHelper.IS_READ))); msg.setTimesTamp(c.getString(c.getColumnIndex(IMDbHelper.TIMES_TAMP))); msg.setIsReadacked(c.getInt(c.getColumnIndex(IMDbHelper.IS_READACKED))); msg.setIsDeliveredacked(c.getInt(c.getColumnIndex(IMDbHelper.IS_DELIVERED_HACKED))); msg.setContentType(c.getInt(c.getColumnIndex(IMDbHelper.CONTENT_TYPE))); msg.setContent(c.getString(c.getColumnIndex(IMDbHelper.CONTENT))); msg.setConversationId(c.getString(c.getColumnIndex(IMDbHelper.CONVERSATION_ID))); msg.setSenderName(c.getString(c.getColumnIndex(IMDbHelper.SENDER_NAME))); msg.setExt(c.getString(c.getColumnIndex(IMDbHelper.EXT))); msg.setDeliveryState(c.getInt(c.getColumnIndex(IMDbHelper.DELIVERY_STATE))); msg.setIsAnonymous(c.getInt(c.getColumnIndex(IMDbHelper.IS_ANONYMOUS))); msg.setMessageType(c.getInt(c.getColumnIndex(IMDbHelper.MESSAGE_TYPE))); msg.setPortraitImg(c.getString(c.getColumnIndex(IMDbHelper.PORTRAIT_IMG))); msg.setDuration(c.getInt(c.getColumnIndex(IMDbHelper.DURATION))); msg.setAudioFilePath(c.getString(c.getColumnIndex(IMDbHelper.AUDIO_FILE_PATH))); msgList.add(msg); } if (null != c) { c.close(); c = null; } } } catch (Exception e) { LogUtil.info(e.toString()); } Collections.reverse(msgList); return msgList; }