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() {
Log.d("xmppService", "inside mark read method");
if (this.messages == null) return;
for(int i = this.messages.size() -1; i >= 0; --i) {
if (messages.get(i).isRead()) return;

View file

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

View file

@ -60,14 +60,14 @@ public class ConversationFragment extends Fragment {
activity.xmppConnectionService.sendMessage(conversation.getAccount(),message);
chatMsg.setText("");
if (conversation.getMode()==Conversation.MODE_SINGLE) {
/*if (conversation.getMode()==Conversation.MODE_SINGLE) {
conversation.getMessages().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);
if (item.getStatus() == Message.STATUS_UNSEND) {
time.setTypeface(null, Typeface.ITALIC);
time.setText("sending\u2026");
} 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()));
} else {
time.setText(item.getCounterpart()+" \u00B7 "+UIHelper.readableTimeDifference(item
.getTimeSent()));
}
}
return view;
}

View file

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