2014-01-24 01:04:05 +00:00
|
|
|
package de.gultsch.chat.services;
|
|
|
|
|
2014-01-30 15:42:35 +00:00
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.UnknownHostException;
|
2014-01-25 18:33:12 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2014-01-30 15:42:35 +00:00
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
|
|
|
2014-01-27 19:40:42 +00:00
|
|
|
import de.gultsch.chat.entities.Account;
|
|
|
|
import de.gultsch.chat.entities.Contact;
|
2014-01-25 18:33:12 +00:00
|
|
|
import de.gultsch.chat.entities.Conversation;
|
|
|
|
import de.gultsch.chat.entities.Message;
|
|
|
|
import de.gultsch.chat.persistance.DatabaseBackend;
|
2014-01-30 15:42:35 +00:00
|
|
|
import de.gultsch.chat.xml.Tag;
|
|
|
|
import de.gultsch.chat.xml.XmlReader;
|
|
|
|
import de.gultsch.chat.xmpp.XmppConnection;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.app.Service;
|
2014-01-30 15:42:35 +00:00
|
|
|
import android.content.Context;
|
2014-01-24 01:04:05 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Binder;
|
|
|
|
import android.os.IBinder;
|
2014-01-30 15:42:35 +00:00
|
|
|
import android.os.PowerManager;
|
2014-01-25 18:33:12 +00:00
|
|
|
import android.util.Log;
|
2014-01-24 01:04:05 +00:00
|
|
|
|
|
|
|
public class XmppConnectionService extends Service {
|
2014-01-25 18:33:12 +00:00
|
|
|
|
2014-01-30 15:42:35 +00:00
|
|
|
protected static final String LOGTAG = "xmppService";
|
2014-01-25 18:33:12 +00:00
|
|
|
protected DatabaseBackend databaseBackend;
|
2014-01-30 15:42:35 +00:00
|
|
|
|
|
|
|
public long startDate;
|
|
|
|
|
|
|
|
private List<Account> accounts;
|
|
|
|
|
|
|
|
public boolean connectionRunnig = false;
|
2014-01-24 01:04:05 +00:00
|
|
|
|
|
|
|
private final IBinder mBinder = new XmppConnectionBinder();
|
|
|
|
|
|
|
|
public class XmppConnectionBinder extends Binder {
|
2014-01-25 18:33:12 +00:00
|
|
|
public XmppConnectionService getService() {
|
|
|
|
return XmppConnectionService.this;
|
2014-01-24 01:04:05 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-25 18:33:12 +00:00
|
|
|
|
2014-01-30 15:42:35 +00:00
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
Log.d(LOGTAG,"recieved start command. been running for "+((System.currentTimeMillis() - startDate) / 1000)+"s");
|
|
|
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
|
|
|
if (!connectionRunnig) {
|
|
|
|
for(Account account : accounts) {
|
|
|
|
Log.d(LOGTAG,"connection wasnt running");
|
|
|
|
XmppConnection connection = new XmppConnection(account, pm);
|
|
|
|
Thread thread = new Thread(connection);
|
|
|
|
thread.start();
|
|
|
|
}
|
|
|
|
connectionRunnig = true;
|
|
|
|
}
|
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
|
2014-01-25 18:33:12 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
databaseBackend = DatabaseBackend.getInstance(getApplicationContext());
|
2014-01-30 15:42:35 +00:00
|
|
|
this.accounts = databaseBackend.getAccounts();
|
|
|
|
startDate = System.currentTimeMillis();
|
2014-01-25 18:33:12 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 01:04:05 +00:00
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
return mBinder;
|
|
|
|
}
|
2014-01-25 18:33:12 +00:00
|
|
|
|
|
|
|
public void sendMessage(Message message) {
|
2014-01-27 19:40:42 +00:00
|
|
|
databaseBackend.createMessage(message);
|
2014-01-25 18:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addConversation(Conversation conversation) {
|
2014-01-27 19:40:42 +00:00
|
|
|
databaseBackend.createConversation(conversation);
|
2014-01-25 18:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<Conversation> getConversations(int status) {
|
|
|
|
return databaseBackend.getConversations(status);
|
|
|
|
}
|
2014-01-27 19:40:42 +00:00
|
|
|
|
2014-01-28 18:21:54 +00:00
|
|
|
public List<Account> getAccounts() {
|
|
|
|
return databaseBackend.getAccounts();
|
|
|
|
}
|
|
|
|
|
2014-01-27 19:40:42 +00:00
|
|
|
public List<Message> getMessages(Conversation conversation) {
|
|
|
|
return databaseBackend.getMessages(conversation, 100);
|
|
|
|
}
|
2014-01-24 01:04:05 +00:00
|
|
|
|
2014-01-27 19:40:42 +00:00
|
|
|
public Conversation findOrCreateConversation(Account account, Contact contact) {
|
|
|
|
Conversation conversation = databaseBackend.findConversation(account, contact);
|
|
|
|
if (conversation!=null) {
|
|
|
|
Log.d("gultsch","found one. unarchive it");
|
|
|
|
conversation.setStatus(Conversation.STATUS_AVAILABLE);
|
|
|
|
this.databaseBackend.updateConversation(conversation);
|
|
|
|
} else {
|
|
|
|
conversation = new Conversation(contact.getDisplayName(), contact.getProfilePhoto(), account, contact.getJid());
|
|
|
|
this.databaseBackend.createConversation(conversation);
|
|
|
|
}
|
|
|
|
return conversation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateConversation(Conversation conversation) {
|
|
|
|
this.databaseBackend.updateConversation(conversation);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getConversationCount() {
|
|
|
|
return this.databaseBackend.getConversationCount();
|
|
|
|
}
|
2014-01-28 18:21:54 +00:00
|
|
|
|
|
|
|
public void createAccount(Account account) {
|
|
|
|
databaseBackend.createAccount(account);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateAccount(Account account) {
|
|
|
|
databaseBackend.updateAccount(account);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteAccount(Account account) {
|
|
|
|
databaseBackend.deleteAccount(account);
|
|
|
|
}
|
2014-01-24 01:04:05 +00:00
|
|
|
}
|