fetching roster. fixed sending messages as type chat

This commit is contained in:
Daniel Gultsch 2014-02-02 16:05:15 +01:00
parent 53d9c9997a
commit d365bbe480
12 changed files with 287 additions and 76 deletions

View File

@ -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))
);
}
}

View File

@ -2,26 +2,62 @@ 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() {
@ -29,6 +65,34 @@ public class Contact implements Serializable {
}
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))
);
}
}

View File

@ -111,4 +111,8 @@ public class Message extends AbstractEntity {
this.conversation = conv;
}
public void setStatus(int status) {
this.status = status;
}
}

View File

@ -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);
}
}

View File

@ -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<Contact> contacts = new ArrayList<Contact>();
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<Account> getAccounts() {
return databaseBackend.getAccounts();
return this.accounts;
}
public List<Message> getMessages(Conversation conversation) {

View File

@ -49,7 +49,7 @@ 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());
@ -57,18 +57,21 @@ public class ConversationActivity extends XmppActivity {
@Override
public void run() {
listAdapter.notifyDataSetChanged();
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<Conversation>() {
@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;
@ -277,51 +275,34 @@ public class ConversationActivity extends XmppActivity {
}
@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());
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));
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();
}

View File

@ -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);

View File

@ -51,6 +51,7 @@ public class NewConversationActivity extends XmppActivity {
protected EditText search;
protected String searchString = "";
private TextView contactsHeader;
private List<Account> 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<Contact> roster) {
rosterContacts.addAll(roster);
runOnUiThread(new Runnable() {
@Override
public void run() {
updateAggregatedContacts();
}
});
}
});
}
}
@Override

View File

@ -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<Contact> roster);
}

View File

@ -46,6 +46,10 @@ public class Element {
return false;
}
public List<Element> getChildren() {
return this.children;
}
public String getContent() {
return content;
}

View File

@ -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);
}
@ -11,6 +13,10 @@ public class MessagePacket extends Element {
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;
}
}
}

View File

@ -53,6 +53,8 @@ public class XmppConnection implements Runnable {
private OnIqPacketReceived unregisteredIqListener = null;
private OnMessagePacketReceived messageListener = null;
private String resource = null;
public XmppConnection(Account account, PowerManager pm) {
this.account = account;
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
@ -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) {