offline messages

This commit is contained in:
Daniel Gultsch 2014-02-11 23:55:03 +01:00
parent f6a6982bd0
commit e63109215e
5 changed files with 103 additions and 46 deletions

View file

@ -78,7 +78,6 @@ public class Conversation extends AbstractEntity {
} }
public void markRead() { public void markRead() {
Log.d("xmppService", "inside mark read method");
if (this.messages == null) return; if (this.messages == null) return;
for(int i = this.messages.size() -1; i >= 0; --i) { for(int i = this.messages.size() -1; i >= 0; --i) {
if (messages.get(i).isRead()) return; if (messages.get(i).isRead()) return;

View file

@ -176,8 +176,6 @@ public class DatabaseBackend extends SQLiteOpenHelper {
public void deleteAccount(Account account) { public void deleteAccount(Account account) {
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
String[] args = { account.getUuid() }; String[] args = { account.getUuid() };
Log.d("gultsch", "backend trying to delete account with uuid:"
+ account.getUuid());
db.delete(Account.TABLENAME, Account.UUID + "=?", args); db.delete(Account.TABLENAME, Account.UUID + "=?", args);
} }
@ -263,4 +261,10 @@ public class DatabaseBackend extends SQLiteOpenHelper {
cursor.moveToFirst(); cursor.moveToFirst();
return Contact.fromCursor(cursor); return Contact.fromCursor(cursor);
} }
public void deleteMessage(Message message) {
SQLiteDatabase db = this.getWritableDatabase();
String[] args = { message.getUuid() };
db.delete(Message.TABLENAME, Message.UUID + "=?", args);
}
} }

View file

@ -54,14 +54,14 @@ public class XmppConnectionService extends Service {
private OnConversationListChangedListener convChangedListener = null; private OnConversationListChangedListener convChangedListener = null;
private OnAccountListChangedListener accountChangedListener = null; private OnAccountListChangedListener accountChangedListener = null;
private ContentObserver contactObserver = new ContentObserver(null) { private ContentObserver contactObserver = new ContentObserver(null) {
@Override @Override
public void onChange(boolean selfChange) { public void onChange(boolean selfChange) {
super.onChange(selfChange); super.onChange(selfChange);
Log.d(LOGTAG,"contact list has changed"); Log.d(LOGTAG, "contact list has changed");
mergePhoneContactsWithRoster(); mergePhoneContactsWithRoster();
} }
}; };
private final IBinder mBinder = new XmppConnectionBinder(); private final IBinder mBinder = new XmppConnectionBinder();
@ -129,15 +129,18 @@ public class XmppConnectionService extends Service {
Message.ENCRYPTION_NONE, status); Message.ENCRYPTION_NONE, status);
if (packet.hasChild("delay")) { if (packet.hasChild("delay")) {
try { try {
String stamp = packet.findChild("delay").getAttribute("stamp"); String stamp = packet.findChild("delay").getAttribute(
stamp = stamp.replace("Z","+0000"); "stamp");
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(stamp); stamp = stamp.replace("Z", "+0000");
Date date = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ssZ").parse(stamp);
message.setTime(date.getTime()); message.setTime(date.getTime());
} catch (ParseException e) { } catch (ParseException e) {
Log.d(LOGTAG,"error trying to parse date"+e.getMessage()); Log.d(LOGTAG,
"error trying to parse date" + e.getMessage());
} }
} }
if(notify) { if (notify) {
message.markUnread(); message.markUnread();
} }
conversation.getMessages().add(message); conversation.getMessages().add(message);
@ -165,6 +168,15 @@ public class XmppConnectionService extends Service {
if (account.getStatus() == Account.STATUS_ONLINE) { if (account.getStatus() == Account.STATUS_ONLINE) {
databaseBackend.clearPresences(account); databaseBackend.clearPresences(account);
connectMultiModeConversations(account); connectMultiModeConversations(account);
List<Conversation> conversations = getConversations();
for(int i = 0; i < conversations.size(); ++i) {
if (conversations.get(i).getAccount()==account) {
sendUnsendMessages(conversations.get(i));
}
}
if (convChangedListener!=null) {
convChangedListener.onConversationListChanged();
}
} }
} }
}; };
@ -233,10 +245,9 @@ public class XmppConnectionService extends Service {
public void onCreate() { public void onCreate() {
databaseBackend = DatabaseBackend.getInstance(getApplicationContext()); databaseBackend = DatabaseBackend.getInstance(getApplicationContext());
this.accounts = databaseBackend.getAccounts(); this.accounts = databaseBackend.getAccounts();
getContentResolver() getContentResolver().registerContentObserver(
.registerContentObserver( ContactsContract.Contacts.CONTENT_URI, true, contactObserver);
ContactsContract.Contacts.CONTENT_URI, true,contactObserver);
} }
public XmppConnection createConnection(Account account) { public XmppConnection createConnection(Account account) {
@ -250,10 +261,53 @@ public class XmppConnectionService extends Service {
return connection; return connection;
} }
public void sendMessage(final Account account, final Message message) { public void sendMessage(Account account, Message message) {
if (message.getConversation().getMode() == Conversation.MODE_SINGLE) {
if (account.getStatus() == Account.STATUS_ONLINE) {
MessagePacket packet = prepareMessagePacket(account, message);
connections.get(account).sendMessagePacket(packet);
if (message.getConversation().getMode() == Conversation.MODE_SINGLE) {
message.setStatus(Message.STATUS_SEND);
if (message.getConversation().getMode() == Conversation.MODE_SINGLE) {
databaseBackend.createMessage(message);
message.getConversation().getMessages().add(message);
if (convChangedListener!=null) {
convChangedListener.onConversationListChanged();
}
}
}
} else {
message.getConversation().getMessages().add(message);
databaseBackend.createMessage(message); databaseBackend.createMessage(message);
if (convChangedListener!=null) {
convChangedListener.onConversationListChanged();
}
} }
}
private void sendUnsendMessages(Conversation conversation) {
for (int i = 0; i < conversation.getMessages().size(); ++i) {
if (conversation.getMessages().get(i).getStatus() == Message.STATUS_UNSEND) {
Message message = conversation.getMessages()
.get(i);
MessagePacket packet = prepareMessagePacket(
conversation.getAccount(),message);
connections.get(conversation.getAccount()).sendMessagePacket(
packet);
message.setStatus(Message.STATUS_SEND);
if (conversation.getMode() == Conversation.MODE_SINGLE) {
databaseBackend.updateMessage(message);
} else {
databaseBackend.deleteMessage(message);
conversation.getMessages().remove(i);
i--;
}
}
}
}
private MessagePacket prepareMessagePacket(Account account, Message message) {
MessagePacket packet = new MessagePacket(); MessagePacket packet = new MessagePacket();
if (message.getConversation().getMode() == Conversation.MODE_SINGLE) { if (message.getConversation().getMode() == Conversation.MODE_SINGLE) {
packet.setType(MessagePacket.TYPE_CHAT); packet.setType(MessagePacket.TYPE_CHAT);
@ -263,13 +317,7 @@ public class XmppConnectionService extends Service {
packet.setTo(message.getCounterpart()); packet.setTo(message.getCounterpart());
packet.setFrom(account.getJid()); packet.setFrom(account.getJid());
packet.setBody(message.getBody()); packet.setBody(message.getBody());
if (account.getStatus() == Account.STATUS_ONLINE) { return packet;
connections.get(account).sendMessagePacket(packet);
if (message.getConversation().getMode() == Conversation.MODE_SINGLE) {
message.setStatus(Message.STATUS_SEND);
databaseBackend.updateMessage(message);
}
}
} }
public void getRoster(Account account, public void getRoster(Account account,
@ -419,20 +467,21 @@ public class XmppConnectionService extends Service {
public List<Account> getAccounts() { public List<Account> getAccounts() {
return this.accounts; return this.accounts;
} }
public Contact findContact(Account account, String jid) { public Contact findContact(Account account, String jid) {
return databaseBackend.findContact(account, jid); return databaseBackend.findContact(account, jid);
} }
public Conversation findOrCreateConversation(Account account, public Conversation findOrCreateConversation(Account account, String jid,
String jid, boolean muc) { boolean muc) {
for (Conversation conv : this.getConversations()) { for (Conversation conv : this.getConversations()) {
if ((conv.getAccount().equals(account)) if ((conv.getAccount().equals(account))
&& (conv.getContactJid().equals(jid))) { && (conv.getContactJid().equals(jid))) {
return conv; return conv;
} }
} }
Conversation conversation = databaseBackend.findConversation(account,jid); Conversation conversation = databaseBackend.findConversation(account,
jid);
if (conversation != null) { if (conversation != null) {
conversation.setStatus(Conversation.STATUS_AVAILABLE); conversation.setStatus(Conversation.STATUS_AVAILABLE);
conversation.setAccount(account); conversation.setAccount(account);
@ -448,13 +497,13 @@ public class XmppConnectionService extends Service {
} else { } else {
String conversationName; String conversationName;
Contact contact = findContact(account, jid); Contact contact = findContact(account, jid);
if (contact!=null) { if (contact != null) {
conversationName = contact.getDisplayName(); conversationName = contact.getDisplayName();
} else { } else {
conversationName = jid.split("@")[0]; conversationName = jid.split("@")[0];
} }
if (muc) { if (muc) {
conversation = new Conversation(conversationName,account, jid, conversation = new Conversation(conversationName, account, jid,
Conversation.MODE_MULTI); Conversation.MODE_MULTI);
if (account.getStatus() == Account.STATUS_ONLINE) { if (account.getStatus() == Account.STATUS_ONLINE) {
joinMuc(account, conversation); joinMuc(account, conversation);
@ -516,9 +565,9 @@ public class XmppConnectionService extends Service {
Log.d(LOGTAG, "found connection. disconnecting"); Log.d(LOGTAG, "found connection. disconnecting");
this.connections.get(account).disconnect(); this.connections.get(account).disconnect();
this.connections.remove(account); this.connections.remove(account);
this.accounts.remove(account);
} }
databaseBackend.deleteAccount(account); databaseBackend.deleteAccount(account);
this.accounts.remove(account);
if (accountChangedListener != null) if (accountChangedListener != null)
accountChangedListener.onAccountListChangedListener(); accountChangedListener.onAccountListChangedListener();
} }
@ -558,15 +607,15 @@ public class XmppConnectionService extends Service {
packet.setAttribute("to", muc + "/" + account.getUsername()); packet.setAttribute("to", muc + "/" + account.getUsername());
Element x = new Element("x"); Element x = new Element("x");
x.setAttribute("xmlns", "http://jabber.org/protocol/muc"); x.setAttribute("xmlns", "http://jabber.org/protocol/muc");
if (conversation.getMessages().size()!=0) { if (conversation.getMessages().size() != 0) {
Element history = new Element("history"); Element history = new Element("history");
history.setAttribute("seconds",(System.currentTimeMillis() - conversation.getLatestMessageDate()) / 1000+""); history.setAttribute(
"seconds",
(System.currentTimeMillis() - conversation
.getLatestMessageDate()) / 1000 + "");
x.addChild(history); x.addChild(history);
} else {
Log.d(LOGTAG,"conversation had no prior messages"+conversation.getMessages().size());
} }
packet.addChild(x); packet.addChild(x);
Log.d(LOGTAG,packet.toString());
connections.get(conversation.getAccount()).sendPresencePacket(packet); connections.get(conversation.getAccount()).sendPresencePacket(packet);
} }

View file

@ -60,14 +60,14 @@ public class ConversationFragment extends Fragment {
activity.xmppConnectionService.sendMessage(conversation.getAccount(),message); activity.xmppConnectionService.sendMessage(conversation.getAccount(),message);
chatMsg.setText(""); chatMsg.setText("");
if (conversation.getMode()==Conversation.MODE_SINGLE) { /*if (conversation.getMode()==Conversation.MODE_SINGLE) {
conversation.getMessages().add(message); conversation.getMessages().add(message);
messageList.add(message); messageList.add(message);
} }*/
activity.updateConversationList(); //activity.updateConversationList();
messagesView.setSelection(messageList.size() - 1); //messagesView.setSelection(messageList.size() - 1);
} }
}); });
@ -151,9 +151,16 @@ public class ConversationFragment extends Fragment {
TextView time = (TextView) view.findViewById(R.id.message_time); TextView time = (TextView) view.findViewById(R.id.message_time);
if (item.getStatus() == Message.STATUS_UNSEND) { if (item.getStatus() == Message.STATUS_UNSEND) {
time.setTypeface(null, Typeface.ITALIC); time.setTypeface(null, Typeface.ITALIC);
time.setText("sending\u2026");
} else { } else {
time.setText(UIHelper.readableTimeDifference(item time.setTypeface(null,Typeface.NORMAL);
if ((item.getConversation().getMode()==Conversation.MODE_SINGLE)||(type != RECIEVED)) {
time.setText(UIHelper.readableTimeDifference(item
.getTimeSent())); .getTimeSent()));
} else {
time.setText(item.getCounterpart()+" \u00B7 "+UIHelper.readableTimeDifference(item
.getTimeSent()));
}
} }
return view; return view;
} }

View file

@ -252,7 +252,6 @@ public class NewConversationActivity extends XmppActivity {
this.accounts = xmppConnectionService.getAccounts(); this.accounts = xmppConnectionService.getAccounts();
this.rosterContacts.clear(); this.rosterContacts.clear();
for (int i = 0; i < accounts.size(); ++i) { for (int i = 0; i < accounts.size(); ++i) {
if (accounts.get(i).getStatus() == Account.STATUS_ONLINE) {
xmppConnectionService.getRoster(accounts.get(i), xmppConnectionService.getRoster(accounts.get(i),
new OnRosterFetchedListener() { new OnRosterFetchedListener() {
@ -271,7 +270,6 @@ public class NewConversationActivity extends XmppActivity {
}); });
} }
} }
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {