From d365bbe480bdd26a042b425c45227785ab4fbf51 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 2 Feb 2014 16:05:15 +0100 Subject: [PATCH] fetching roster. fixed sending messages as type chat --- src/de/gultsch/chat/entities/Account.java | 15 +++- src/de/gultsch/chat/entities/Contact.java | 88 ++++++++++++++++--- src/de/gultsch/chat/entities/Message.java | 4 + .../chat/persistance/DatabaseBackend.java | 33 +++++-- .../chat/services/XmppConnectionService.java | 65 +++++++++++++- .../gultsch/chat/ui/ConversationActivity.java | 68 ++++++-------- .../gultsch/chat/ui/ConversationFragment.java | 8 +- .../chat/ui/NewConversationActivity.java | 25 +++++- .../chat/ui/OnRosterFetchedListener.java | 8 ++ src/de/gultsch/chat/xml/Element.java | 4 + src/de/gultsch/chat/xmpp/MessagePacket.java | 32 +++++++ src/de/gultsch/chat/xmpp/XmppConnection.java | 13 +++ 12 files changed, 287 insertions(+), 76 deletions(-) create mode 100644 src/de/gultsch/chat/ui/OnRosterFetchedListener.java diff --git a/src/de/gultsch/chat/entities/Account.java b/src/de/gultsch/chat/entities/Account.java index 5f95af638..dcb5516a4 100644 --- a/src/de/gultsch/chat/entities/Account.java +++ b/src/de/gultsch/chat/entities/Account.java @@ -13,10 +13,14 @@ public class Account extends AbstractEntity{ public static final String USERNAME = "username"; public static final String SERVER = "server"; public static final String PASSWORD = "password"; + public static final String OPTIONS = "options"; + public static final String ROSTERVERSION = "rosterversion"; protected String username; protected String server; protected String password; + protected int options; + protected String rosterVersion; protected boolean online = false; @@ -25,13 +29,15 @@ public class Account extends AbstractEntity{ } public Account(String username, String server, String password) { - this(java.util.UUID.randomUUID().toString(),username,server,password); + this(java.util.UUID.randomUUID().toString(),username,server,password,0,null); } - public Account(String uuid, String username, String server,String password) { + public Account(String uuid, String username, String server,String password, int options, String rosterVersion) { this.uuid = uuid; this.username = username; this.server = server; this.password = password; + this.options = options; + this.rosterVersion = rosterVersion; } public String getUsername() { @@ -80,7 +86,10 @@ public class Account extends AbstractEntity{ return new Account(cursor.getString(cursor.getColumnIndex(UUID)), cursor.getString(cursor.getColumnIndex(USERNAME)), cursor.getString(cursor.getColumnIndex(SERVER)), - cursor.getString(cursor.getColumnIndex(PASSWORD))); + cursor.getString(cursor.getColumnIndex(PASSWORD)), + cursor.getInt(cursor.getColumnIndex(OPTIONS)), + cursor.getString(cursor.getColumnIndex(ROSTERVERSION)) + ); } } diff --git a/src/de/gultsch/chat/entities/Contact.java b/src/de/gultsch/chat/entities/Contact.java index 66221f8a3..f235fb4dc 100644 --- a/src/de/gultsch/chat/entities/Contact.java +++ b/src/de/gultsch/chat/entities/Contact.java @@ -2,33 +2,97 @@ package de.gultsch.chat.entities; import java.io.Serializable; -import android.net.Uri; +import android.content.ContentValues; +import android.database.Cursor; -public class Contact implements Serializable { +public class Contact extends AbstractEntity implements Serializable { private static final long serialVersionUID = -4570817093119419962L; - protected String display_name; - protected String jid; - protected String photo; - public Contact(String display_name, String jid, String photo) { - this.display_name = display_name; + + public static final String TABLENAME = "contacts"; + + public static final String DISPLAYNAME = "name"; + public static final String JID = "jid"; + public static final String SUBSCRIPTION = "subscription"; + public static final String SYSTEMACCOUNT = "systemaccount"; + public static final String PHOTOURI = "photouri"; + public static final String OPENPGPKEY = "pgpkey"; + public static final String LASTONLINEPRESENCE = "presence"; + public static final String ACCOUNT = "accountUuid"; + + protected String accountUuid; + protected String displayName; + protected String jid; + protected String subscription; + protected int systemAccount; + protected String photoUri; + protected String openPGPKey; + protected long lastOnlinePresence; + + public Contact(Account account, String displayName, String jid, String photoUri) { + if (account == null) { + this.accountUuid = null; + } else { + this.accountUuid = account.getUuid(); + } + this.displayName = displayName; this.jid = jid; - this.photo = photo; + this.photoUri = photoUri; + } + + public Contact(String uuid, String account, String displayName, String jid, String subscription, String photoUri, int systemAccount, String pgpKey, long lastseen) { + this.uuid = uuid; + this.accountUuid = account; + this.displayName = displayName; + this.jid = jid; + this.subscription = subscription; + this.photoUri = photoUri; + this.systemAccount = systemAccount; + this.openPGPKey = pgpKey; + this.lastOnlinePresence = lastseen; } public String getDisplayName() { - return this.display_name; + return this.displayName; } public String getProfilePhoto() { - return photo; + return this.photoUri; } - + public String getJid() { return this.jid; } public boolean match(String needle) { - return (jid.toLowerCase().contains(needle.toLowerCase()) || (display_name.toLowerCase().contains(needle.toLowerCase()))); + return (jid.toLowerCase().contains(needle.toLowerCase()) || (displayName.toLowerCase().contains(needle.toLowerCase()))); + } + + @Override + public ContentValues getContentValues() { + ContentValues values = new ContentValues(); + values.put(UUID,uuid); + values.put(ACCOUNT,accountUuid); + values.put(DISPLAYNAME, displayName); + values.put(JID, jid); + values.put(SUBSCRIPTION,subscription); + values.put(SYSTEMACCOUNT, systemAccount); + values.put(PHOTOURI,photoUri); + values.put(OPENPGPKEY,openPGPKey); + values.put(LASTONLINEPRESENCE,lastOnlinePresence); + return values; + } + + public static Contact fromCursor(Cursor cursor) { + return new Contact(cursor.getString(cursor.getColumnIndex(UUID)), + cursor.getString(cursor.getColumnIndex(ACCOUNT)), + cursor.getString(cursor.getColumnIndex(DISPLAYNAME)), + cursor.getString(cursor.getColumnIndex(JID)), + cursor.getString(cursor.getColumnIndex(SUBSCRIPTION)), + cursor.getString(cursor.getColumnIndex(PHOTOURI)), + cursor.getInt(cursor.getColumnIndex(SYSTEMACCOUNT)), + cursor.getString(cursor.getColumnIndex(OPENPGPKEY)), + cursor.getLong(cursor.getColumnIndex(LASTONLINEPRESENCE)) + ); } } diff --git a/src/de/gultsch/chat/entities/Message.java b/src/de/gultsch/chat/entities/Message.java index 530a4db42..4349e3412 100644 --- a/src/de/gultsch/chat/entities/Message.java +++ b/src/de/gultsch/chat/entities/Message.java @@ -111,4 +111,8 @@ public class Message extends AbstractEntity { this.conversation = conv; } + public void setStatus(int status) { + this.status = status; + } + } diff --git a/src/de/gultsch/chat/persistance/DatabaseBackend.java b/src/de/gultsch/chat/persistance/DatabaseBackend.java index 10dd0b6e0..bb2091123 100644 --- a/src/de/gultsch/chat/persistance/DatabaseBackend.java +++ b/src/de/gultsch/chat/persistance/DatabaseBackend.java @@ -28,21 +28,35 @@ public class DatabaseBackend extends SQLiteOpenHelper { public void onCreate(SQLiteDatabase db) { db.execSQL("PRAGMA foreign_keys=ON;"); db.execSQL("create table " + Account.TABLENAME + "(" + Account.UUID - + " TEXT PRIMARY KEY," + Account.USERNAME + " TEXT," + Account.SERVER - + " TEXT," + Account.PASSWORD + " TEXT)"); + + " TEXT PRIMARY KEY," + Account.USERNAME + " TEXT," + + Account.SERVER + " TEXT," + Account.PASSWORD + " TEXT," + + Account.ROSTERVERSION + " TEXT," + Account.OPTIONS + + " NUMBER)"); db.execSQL("create table " + Conversation.TABLENAME + " (" + Conversation.UUID + " TEXT PRIMARY KEY, " + Conversation.NAME + " TEXT, " + Conversation.PHOTO_URI + " TEXT, " + Conversation.ACCOUNT + " TEXT, " + Conversation.CONTACT + " TEXT, " + Conversation.CREATED + " NUMBER, " - + Conversation.STATUS + " NUMBER," - + "FOREIGN KEY("+Conversation.ACCOUNT+") REFERENCES "+Account.TABLENAME+"("+Account.UUID+") ON DELETE CASCADE);"); + + Conversation.STATUS + " NUMBER," + "FOREIGN KEY(" + + Conversation.ACCOUNT + ") REFERENCES " + Account.TABLENAME + + "(" + Account.UUID + ") ON DELETE CASCADE);"); db.execSQL("create table " + Message.TABLENAME + "( " + Message.UUID + " TEXT PRIMARY KEY, " + Message.CONVERSATION + " TEXT, " + Message.TIME_SENT + " NUMBER, " + Message.COUNTERPART + " TEXT, " + Message.BODY + " TEXT, " + Message.ENCRYPTION - + " NUMBER, " + Message.STATUS + " NUMBER," - + "FOREIGN KEY("+Message.CONVERSATION+") REFERENCES "+Conversation.TABLENAME+"("+Message.UUID+") ON DELETE CASCADE);"); + + " NUMBER, " + Message.STATUS + " NUMBER," + "FOREIGN KEY(" + + Message.CONVERSATION + ") REFERENCES " + + Conversation.TABLENAME + "(" + Conversation.UUID + + ") ON DELETE CASCADE);"); + db.execSQL("create table " + Contact.TABLENAME + "(" + Contact.UUID + + " TEXT PRIMARY KEY, " + Contact.ACCOUNT + " TEXT, " + + Contact.DISPLAYNAME + " TEXT," + Contact.JID + " TEXT," + + Contact.LASTONLINEPRESENCE + " NUMBER, " + Contact.OPENPGPKEY + + " TEXT," + Contact.PHOTOURI + " TEXT," + Contact.SUBSCRIPTION + + " TEXT," + Contact.SYSTEMACCOUNT + " NUMBER, " + + "FOREIGN KEY(" + Contact.ACCOUNT + ") REFERENCES " + + Account.TABLENAME + "(" + Account.UUID + + ") ON DELETE CASCADE);"); } @Override @@ -163,4 +177,11 @@ public class DatabaseBackend extends SQLiteOpenHelper { db.execSQL("PRAGMA foreign_keys=ON;"); return db; } + + public void updateMessage(Message message) { + SQLiteDatabase db = this.getWritableDatabase(); + String[] args = { message.getUuid() }; + db.update(Message.TABLENAME, message.getContentValues(), Message.UUID + + "=?", args); + } } diff --git a/src/de/gultsch/chat/services/XmppConnectionService.java b/src/de/gultsch/chat/services/XmppConnectionService.java index f2eedbe7e..2fc3c937e 100644 --- a/src/de/gultsch/chat/services/XmppConnectionService.java +++ b/src/de/gultsch/chat/services/XmppConnectionService.java @@ -1,5 +1,7 @@ package de.gultsch.chat.services; +import java.io.IOException; +import java.util.ArrayList; import java.util.Hashtable; import java.util.List; @@ -10,7 +12,11 @@ import de.gultsch.chat.entities.Conversation; import de.gultsch.chat.entities.Message; import de.gultsch.chat.persistance.DatabaseBackend; import de.gultsch.chat.ui.OnConversationListChangedListener; +import de.gultsch.chat.ui.OnRosterFetchedListener; +import de.gultsch.chat.xml.Element; +import de.gultsch.chat.xmpp.IqPacket; import de.gultsch.chat.xmpp.MessagePacket; +import de.gultsch.chat.xmpp.OnIqPacketReceived; import de.gultsch.chat.xmpp.OnMessagePacketReceived; import de.gultsch.chat.xmpp.XmppConnection; import android.app.Service; @@ -45,7 +51,7 @@ public class XmppConnectionService extends Service { String name = jid.split("@")[0]; Log.d(LOGTAG,"message received for "+account.getJid()+" from "+jid); Log.d(LOGTAG,packet.toString()); - Contact contact = new Contact(name,jid,null); //dummy contact + Contact contact = new Contact(account,name,jid,null); //dummy contact Conversation conversation = findOrCreateConversation(account, contact); Message message = new Message(conversation, fullJid, packet.getBody(), Message.ENCRYPTION_NONE, Message.STATUS_RECIEVED); conversation.getMessages().add(message); @@ -88,8 +94,59 @@ public class XmppConnectionService extends Service { return mBinder; } - public void sendMessage(Message message) { - databaseBackend.createMessage(message); + public void sendMessage(final Account account, final Message message) { + new Thread() { + @Override + public void run() { + Log.d(LOGTAG,"sending message for "+account.getJid()+" to: "+message.getCounterpart()); + databaseBackend.createMessage(message); + MessagePacket packet = new MessagePacket(); + packet.setType(MessagePacket.TYPE_CHAT); + packet.setTo(message.getCounterpart()); + packet.setFrom(account.getJid()); + packet.setBody(message.getBody()); + try { + connections.get(account).sendMessagePacket(packet); + message.setStatus(Message.STATUS_SEND); + databaseBackend.updateMessage(message); + } catch (IOException e) { + Log.d(LOGTAG,"io exception during send. message is in database. will try again later"); + } + } + }.start(); + } + + public void getRoster(final Account account, final OnRosterFetchedListener listener) { + IqPacket iqPacket = new IqPacket(IqPacket.TYPE_GET); + Element query = new Element("query"); + query.setAttribute("xmlns", "jabber:iq:roster"); + query.setAttribute("ver", ""); + iqPacket.addChild(query); + try { + connections.get(account).sendIqPacket(iqPacket, new OnIqPacketReceived() { + + @Override + public void onIqPacketReceived(Account account, IqPacket packet) { + Element roster = packet.findChild("query"); + Log.d(LOGTAG,roster.toString()); + List contacts = new ArrayList(); + for(Element item : roster.getChildren()) { + String name = item.getAttribute("name"); + String jid = item.getAttribute("jid"); + if (name==null) { + name = jid.split("@")[0]; + } + Contact contact = new Contact(account, name, jid, null); + contacts.add(contact); + } + if (listener != null) { + listener.onRosterFetched(contacts); + } + } + }); + } catch (IOException e) { + Log.d(LOGTAG,"io error during roster fetch"); + } } public void addConversation(Conversation conversation) { @@ -111,7 +168,7 @@ public class XmppConnectionService extends Service { } public List getAccounts() { - return databaseBackend.getAccounts(); + return this.accounts; } public List getMessages(Conversation conversation) { diff --git a/src/de/gultsch/chat/ui/ConversationActivity.java b/src/de/gultsch/chat/ui/ConversationActivity.java index 23d03a015..b06fa21d1 100644 --- a/src/de/gultsch/chat/ui/ConversationActivity.java +++ b/src/de/gultsch/chat/ui/ConversationActivity.java @@ -49,26 +49,29 @@ public class ConversationActivity extends XmppActivity { @Override public void onConversationListChanged() { - Log.d("xmppService","on conversation list changed event received"); + final Conversation currentConv = conversationList.get(selectedConversation); conversationList.clear(); conversationList.addAll(xmppConnectionService .getConversations()); runOnUiThread(new Runnable() { @Override - public void run() { - listAdapter.notifyDataSetChanged(); + public void run() { + updateConversationList(); + for(int i = 0; i < conversationList.size(); ++i) { + if (currentConv.equals(conversationList.get(i))) { + selectedConversation = i; + break; + } + } if(paneShouldBeOpen) { selectedConversation = 0; if (conversationList.size() >= 1) { - updateConversationList(); swapConversationFragment(); } else { startActivity(new Intent(getApplicationContext(), NewConversationActivity.class)); finish(); } - } else { - Log.d("xmppService","pane wasnt open. dont swap fragment"); } } }); @@ -98,19 +101,12 @@ public class ConversationActivity extends XmppActivity { public void updateConversationList() { if (conversationList.size() >= 1) { - Conversation currentConv = conversationList.get(selectedConversation); Collections.sort(this.conversationList, new Comparator() { @Override public int compare(Conversation lhs, Conversation rhs) { return (int) (rhs.getLatestMessageDate() - lhs.getLatestMessageDate()); } }); - for(int i = 0; i < conversationList.size(); ++i) { - if (currentConv == conversationList.get(i)) { - selectedConversation = i; - break; - } - } } this.listView.invalidateViews(); } @@ -198,6 +194,7 @@ public class ConversationActivity extends XmppActivity { @Override public void onPanelClosed(View arg0) { + paneShouldBeOpen = false; if (conversationList.size() > 0) { getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setTitle(conversationList.get(selectedConversation).getName()); @@ -248,6 +245,7 @@ public class ConversationActivity extends XmppActivity { paneShouldBeOpen = true; spl.openPane(); xmppConnectionService.archiveConversation(conv); + selectedConversation = 0; break; default: break; @@ -275,53 +273,36 @@ public class ConversationActivity extends XmppActivity { } return super.onKeyDown(keyCode, event); } - - @Override - public void onStart() { - super.onStart(); - if (xmppConnectionServiceBound) { - conversationList.clear(); - conversationList.addAll(xmppConnectionService - .getConversations()); - } - } @Override - public void onPause() { + protected void onPause() { super.onPause(); if (xmppConnectionServiceBound) { - Log.d("xmppService","called on pause. remove listener"); - xmppConnectionService.removeOnConversationListChangedListener(); - } - } - - @Override - protected void onStop() { - super.onStop(); - if (xmppConnectionServiceBound) { Log.d("xmppService","called on stop. remove listener"); xmppConnectionService.removeOnConversationListChangedListener(); unbindService(mConnection); xmppConnectionServiceBound = false; } - } - + } @Override void onBackendConnected() { xmppConnectionService.setOnConversationListChangedListener(this.onConvChanged); - conversationList.clear(); - conversationList.addAll(xmppConnectionService - .getConversations()); - - for(Conversation conversation : conversationList) { - conversation.setMessages(xmppConnectionService.getMessages(conversation)); + if (conversationList.size()==0) { + Log.d("gultsch","conversation list is empty fetch new"); + conversationList.clear(); + conversationList.addAll(xmppConnectionService + .getConversations()); + + for(Conversation conversation : conversationList) { + conversation.setMessages(xmppConnectionService.getMessages(conversation)); + } + + this.updateConversationList(); } - this.updateConversationList(); - if ((getIntent().getAction().equals(Intent.ACTION_VIEW) && (!handledViewIntent))) { if (getIntent().getType().equals( ConversationActivity.VIEW_CONVERSATION)) { @@ -354,6 +335,7 @@ public class ConversationActivity extends XmppActivity { selectedFragment.onBackendConnected(); } else { Log.d("gultsch","conversationactivity. no old fragment found. creating new one"); + selectedConversation = 0; Log.d("gultsch","selected conversation is #"+selectedConversation); swapConversationFragment(); } diff --git a/src/de/gultsch/chat/ui/ConversationFragment.java b/src/de/gultsch/chat/ui/ConversationFragment.java index 57d4b1ebc..b8edcbaf5 100644 --- a/src/de/gultsch/chat/ui/ConversationFragment.java +++ b/src/de/gultsch/chat/ui/ConversationFragment.java @@ -54,7 +54,7 @@ public class ConversationFragment extends Fragment { return; Message message = new Message(conversation, chatMsg .getText().toString(), Message.ENCRYPTION_NONE); - activity.xmppConnectionService.sendMessage(message); + activity.xmppConnectionService.sendMessage(conversation.getAccount(),message); conversation.getMessages().add(message); chatMsg.setText(""); @@ -105,16 +105,12 @@ public class ConversationFragment extends Fragment { case SENT: view = (View) inflater.inflate(R.layout.message_sent, null); - Log.d("gultsch", "inflated new message_sent view"); break; case RECIEVED: view = (View) inflater.inflate( R.layout.message_recieved, null); - Log.d("gultsch", "inflated new message_recieved view"); break; } - } else { - Log.d("gultsch", "recylecd a view"); } ImageView imageView = (ImageView) view.findViewById(R.id.message_photo); if (type == RECIEVED) { @@ -128,7 +124,7 @@ public class ConversationFragment extends Fragment { imageView.setImageURI(profilePicture); } ((TextView) view.findViewById(R.id.message_body)).setText(item - .getBody()); + .getBody().trim()); TextView time = (TextView) view.findViewById(R.id.message_time); if (item.getStatus() == Message.STATUS_UNSEND) { time.setTypeface(null, Typeface.ITALIC); diff --git a/src/de/gultsch/chat/ui/NewConversationActivity.java b/src/de/gultsch/chat/ui/NewConversationActivity.java index b8061b23e..b41ca7614 100644 --- a/src/de/gultsch/chat/ui/NewConversationActivity.java +++ b/src/de/gultsch/chat/ui/NewConversationActivity.java @@ -51,6 +51,7 @@ public class NewConversationActivity extends XmppActivity { protected EditText search; protected String searchString = ""; private TextView contactsHeader; + private List accounts; protected void updateAggregatedContacts() { @@ -76,7 +77,7 @@ public class NewConversationActivity extends XmppActivity { if (Validator.isValidJid(searchString)) { String name = searchString.split("@")[0]; - Contact newContact = new Contact(name, searchString,null); + Contact newContact = new Contact(null,name, searchString,null); aggregatedContacts.add(newContact); contactsHeader.setText("Create new contact"); } else { @@ -87,6 +88,7 @@ public class NewConversationActivity extends XmppActivity { } contactsAdapter.notifyDataSetChanged(); + contactsView.setScrollX(0); } static final String[] PROJECTION = new String[] { @@ -229,7 +231,7 @@ public class NewConversationActivity extends XmppActivity { /*if (profilePhoto == null) { profilePhoto = DEFAULT_PROFILE_PHOTO; }*/ - Contact contact = new Contact( + Contact contact = new Contact(null, cursor.getString(cursor .getColumnIndex(ContactsContract.Data.DISPLAY_NAME)), cursor.getString(cursor @@ -250,6 +252,25 @@ public class NewConversationActivity extends XmppActivity { getActionBar().setDisplayHomeAsUpEnabled(false); getActionBar().setHomeButtonEnabled(false); } + this.accounts = xmppConnectionService.getAccounts(); + this.rosterContacts.clear(); + for(Account account : this.accounts) { + xmppConnectionService.getRoster(account, new OnRosterFetchedListener() { + + @Override + public void onRosterFetched(List roster) { + rosterContacts.addAll(roster); + runOnUiThread(new Runnable() { + + @Override + public void run() { + updateAggregatedContacts(); + } + }); + + } + }); + } } @Override diff --git a/src/de/gultsch/chat/ui/OnRosterFetchedListener.java b/src/de/gultsch/chat/ui/OnRosterFetchedListener.java new file mode 100644 index 000000000..ad3f6592b --- /dev/null +++ b/src/de/gultsch/chat/ui/OnRosterFetchedListener.java @@ -0,0 +1,8 @@ +package de.gultsch.chat.ui; + +import java.util.List; +import de.gultsch.chat.entities.Contact; + +public interface OnRosterFetchedListener { + public void onRosterFetched(List roster); +} diff --git a/src/de/gultsch/chat/xml/Element.java b/src/de/gultsch/chat/xml/Element.java index 24e656491..01deb1d20 100644 --- a/src/de/gultsch/chat/xml/Element.java +++ b/src/de/gultsch/chat/xml/Element.java @@ -46,6 +46,10 @@ public class Element { return false; } + public List getChildren() { + return this.children; + } + public String getContent() { return content; } diff --git a/src/de/gultsch/chat/xmpp/MessagePacket.java b/src/de/gultsch/chat/xmpp/MessagePacket.java index 313fb355b..de486a5da 100644 --- a/src/de/gultsch/chat/xmpp/MessagePacket.java +++ b/src/de/gultsch/chat/xmpp/MessagePacket.java @@ -3,6 +3,8 @@ package de.gultsch.chat.xmpp; import de.gultsch.chat.xml.Element; public class MessagePacket extends Element { + public static final int TYPE_CHAT = 0; + private MessagePacket(String name) { super(name); } @@ -10,6 +12,10 @@ public class MessagePacket extends Element { public MessagePacket() { super("message"); } + + public String getTo() { + return getAttribute("to"); + } public String getFrom() { return getAttribute("from"); @@ -18,4 +24,30 @@ public class MessagePacket extends Element { public String getBody() { return this.findChild("body").getContent(); } + + public void setTo(String to) { + setAttribute("to", to); + } + + public void setFrom(String from) { + setAttribute("from",from); + } + + public void setBody(String text) { + Element body = new Element("body"); + body.setContent(text); + this.children.add(body); + } + + public void setType(int type) { + switch (type) { + case TYPE_CHAT: + this.setAttribute("type","chat"); + break; + + default: + this.setAttribute("type","chat"); + break; + } + } } diff --git a/src/de/gultsch/chat/xmpp/XmppConnection.java b/src/de/gultsch/chat/xmpp/XmppConnection.java index 441e4b2d3..f82c401e0 100644 --- a/src/de/gultsch/chat/xmpp/XmppConnection.java +++ b/src/de/gultsch/chat/xmpp/XmppConnection.java @@ -52,6 +52,8 @@ public class XmppConnection implements Runnable { private OnPresencePacketReceived presenceListener = null; private OnIqPacketReceived unregisteredIqListener = null; private OnMessagePacketReceived messageListener = null; + + private String resource = null; public XmppConnection(Account account, PowerManager pm) { this.account = account; @@ -96,6 +98,12 @@ public class XmppConnection implements Runnable { public void run() { while(shouldReConnect) { connect(); + try { + Thread.sleep(30000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } } @@ -260,7 +268,9 @@ public class XmppConnection implements Runnable { this.sendIqPacket(iq, new OnIqPacketReceived() { @Override public void onIqPacketReceived(Account account, IqPacket packet) { + resource = packet.findChild("bind").findChild("jid").getContent().split("/")[1]; Log.d(LOGTAG,"answer for our bind was: "+packet.toString()); + Log.d(LOGTAG,"new resource is "+resource); } }); } @@ -291,15 +301,18 @@ public class XmppConnection implements Runnable { if (callback != null) { iqPacketCallbacks.put(id, callback); } + tagWriter.flush(); Log.d(LOGTAG,"sending: "+packet.toString()); } public void sendMessagePacket(MessagePacket packet) throws IOException { tagWriter.writeElement(packet); + tagWriter.flush(); } public void sendPresencePacket(PresencePacket packet) throws IOException { tagWriter.writeElement(packet); + tagWriter.flush(); } public void setOnMessagePacketReceivedListener(OnMessagePacketReceived listener) {