1package de.gultsch.chat.services;
2
3import java.util.List;
4
5import de.gultsch.chat.entities.Account;
6import de.gultsch.chat.entities.Contact;
7import de.gultsch.chat.entities.Conversation;
8import de.gultsch.chat.entities.Message;
9import de.gultsch.chat.persistance.DatabaseBackend;
10import android.app.Service;
11import android.content.Intent;
12import android.os.Binder;
13import android.os.IBinder;
14import android.util.Log;
15
16public class XmppConnectionService extends Service {
17
18 protected static final String LOGTAG = "xmppConnection";
19 protected DatabaseBackend databaseBackend;
20
21 private final IBinder mBinder = new XmppConnectionBinder();
22
23 public class XmppConnectionBinder extends Binder {
24 public XmppConnectionService getService() {
25 return XmppConnectionService.this;
26 }
27 }
28
29 @Override
30 public void onCreate() {
31 databaseBackend = DatabaseBackend.getInstance(getApplicationContext());
32 }
33
34 @Override
35 public IBinder onBind(Intent intent) {
36 return mBinder;
37 }
38
39 public void sendMessage(Message message) {
40 databaseBackend.createMessage(message);
41 }
42
43 public void addConversation(Conversation conversation) {
44 databaseBackend.createConversation(conversation);
45 }
46
47 public List<Conversation> getConversations(int status) {
48 return databaseBackend.getConversations(status);
49 }
50
51 public List<Message> getMessages(Conversation conversation) {
52 return databaseBackend.getMessages(conversation, 100);
53 }
54
55 public Conversation findOrCreateConversation(Account account, Contact contact) {
56 Conversation conversation = databaseBackend.findConversation(account, contact);
57 if (conversation!=null) {
58 Log.d("gultsch","found one. unarchive it");
59 conversation.setStatus(Conversation.STATUS_AVAILABLE);
60 this.databaseBackend.updateConversation(conversation);
61 } else {
62 conversation = new Conversation(contact.getDisplayName(), contact.getProfilePhoto(), account, contact.getJid());
63 this.databaseBackend.createConversation(conversation);
64 }
65 return conversation;
66 }
67
68 public void updateConversation(Conversation conversation) {
69 this.databaseBackend.updateConversation(conversation);
70 }
71
72 public int getConversationCount() {
73 return this.databaseBackend.getConversationCount();
74 }
75}