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<Account> getAccounts() {
52 return databaseBackend.getAccounts();
53 }
54
55 public List<Message> getMessages(Conversation conversation) {
56 return databaseBackend.getMessages(conversation, 100);
57 }
58
59 public Conversation findOrCreateConversation(Account account, Contact contact) {
60 Conversation conversation = databaseBackend.findConversation(account, contact);
61 if (conversation!=null) {
62 Log.d("gultsch","found one. unarchive it");
63 conversation.setStatus(Conversation.STATUS_AVAILABLE);
64 this.databaseBackend.updateConversation(conversation);
65 } else {
66 conversation = new Conversation(contact.getDisplayName(), contact.getProfilePhoto(), account, contact.getJid());
67 this.databaseBackend.createConversation(conversation);
68 }
69 return conversation;
70 }
71
72 public void updateConversation(Conversation conversation) {
73 this.databaseBackend.updateConversation(conversation);
74 }
75
76 public int getConversationCount() {
77 return this.databaseBackend.getConversationCount();
78 }
79
80 public void createAccount(Account account) {
81 databaseBackend.createAccount(account);
82 }
83
84 public void updateAccount(Account account) {
85 databaseBackend.updateAccount(account);
86 }
87
88 public void deleteAccount(Account account) {
89 databaseBackend.deleteAccount(account);
90 }
91}