From 95068ee776d9ec8e6c390133ca2bed083a46546f Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 7 Feb 2014 16:50:29 +0100 Subject: reworked new conversation activity again. less asking more knowing. mucs are now connected on creation of the conversation. no reconnect required --- src/de/gultsch/chat/entities/Contact.java | 59 ++++++--- src/de/gultsch/chat/entities/Conversation.java | 4 + .../chat/services/XmppConnectionService.java | 57 ++++++--- .../gultsch/chat/ui/NewConversationActivity.java | 142 ++++++++++++--------- src/de/gultsch/chat/xmpp/XmppConnection.java | 2 +- 5 files changed, 164 insertions(+), 100 deletions(-) diff --git a/src/de/gultsch/chat/entities/Contact.java b/src/de/gultsch/chat/entities/Contact.java index 3478eac7..6079d9bb 100644 --- a/src/de/gultsch/chat/entities/Contact.java +++ b/src/de/gultsch/chat/entities/Contact.java @@ -7,10 +7,9 @@ import android.database.Cursor; public class Contact extends AbstractEntity implements Serializable { private static final long serialVersionUID = -4570817093119419962L; - - + public static final String TABLENAME = "contacts"; - + public static final String DISPLAYNAME = "name"; public static final String JID = "jid"; public static final String SUBSCRIPTION = "subscription"; @@ -19,7 +18,7 @@ public class Contact extends AbstractEntity implements Serializable { public static final String OPENPGPKEY = "pgpkey"; public static final String LASTPRESENCE = "presence"; public static final String ACCOUNT = "accountUuid"; - + protected String accountUuid; protected String displayName; protected String jid; @@ -29,10 +28,10 @@ public class Contact extends AbstractEntity implements Serializable { protected String openPGPKey; protected long lastPresence; - protected Account account; - - public Contact(Account account, String displayName, String jid, String photoUri) { + + public Contact(Account account, String displayName, String jid, + String photoUri) { if (account == null) { this.accountUuid = null; } else { @@ -42,8 +41,10 @@ public class Contact extends AbstractEntity implements Serializable { this.jid = jid; this.photoUri = photoUri; } - - public Contact(String uuid, String account, String displayName, String jid, String subscription, String photoUri, int systemAccount, String pgpKey, long lastseen) { + + 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; @@ -62,30 +63,31 @@ public class Contact extends AbstractEntity implements Serializable { public String getProfilePhoto() { return this.photoUri; } - + public String getJid() { return this.jid; } - + public boolean match(String needle) { - return (jid.toLowerCase().contains(needle.toLowerCase()) || (displayName.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(UUID, uuid); + values.put(ACCOUNT, accountUuid); values.put(DISPLAYNAME, displayName); values.put(JID, jid); - values.put(SUBSCRIPTION,subscription); + values.put(SUBSCRIPTION, subscription); values.put(SYSTEMACCOUNT, systemAccount); - values.put(PHOTOURI,photoUri); - values.put(OPENPGPKEY,openPGPKey); - values.put(LASTPRESENCE,lastPresence); + values.put(PHOTOURI, photoUri); + values.put(OPENPGPKEY, openPGPKey); + values.put(LASTPRESENCE, lastPresence); return values; } - + public static Contact fromCursor(Cursor cursor) { return new Contact(cursor.getString(cursor.getColumnIndex(UUID)), cursor.getString(cursor.getColumnIndex(ACCOUNT)), @@ -95,8 +97,7 @@ public class Contact extends AbstractEntity implements Serializable { cursor.getString(cursor.getColumnIndex(PHOTOURI)), cursor.getInt(cursor.getColumnIndex(SYSTEMACCOUNT)), cursor.getString(cursor.getColumnIndex(OPENPGPKEY)), - cursor.getLong(cursor.getColumnIndex(LASTPRESENCE)) - ); + cursor.getLong(cursor.getColumnIndex(LASTPRESENCE))); } public void setSubscription(String subscription) { @@ -119,4 +120,20 @@ public class Contact extends AbstractEntity implements Serializable { public void setUuid(String uuid) { this.uuid = uuid; } + + public boolean couldBeMuc() { + String[] split = this.getJid().split("@"); + if (split.length != 2) { + return false; + } else { + String[] domainParts = split[1].split("\\."); + if (domainParts.length < 3) { + return false; + } else { + return (domainParts[0].equals("conf") + || domainParts[0].equals("conference") || domainParts[0] + .equals("muc")); + } + } + } } diff --git a/src/de/gultsch/chat/entities/Conversation.java b/src/de/gultsch/chat/entities/Conversation.java index 1b666ce3..329f2bea 100644 --- a/src/de/gultsch/chat/entities/Conversation.java +++ b/src/de/gultsch/chat/entities/Conversation.java @@ -160,4 +160,8 @@ public class Conversation extends AbstractEntity { public int getMode() { return this.mode; } + + public void setMode(int mode) { + this.mode = mode; + } } diff --git a/src/de/gultsch/chat/services/XmppConnectionService.java b/src/de/gultsch/chat/services/XmppConnectionService.java index 43be24c3..eafb121c 100644 --- a/src/de/gultsch/chat/services/XmppConnectionService.java +++ b/src/de/gultsch/chat/services/XmppConnectionService.java @@ -65,7 +65,7 @@ public class XmppConnectionService extends Service { String jid = fullJid.split("/")[0]; counterPart = fullJid; Contact contact = findOrCreateContact(account,jid); - conversation = findOrCreateConversation(account, contact); + conversation = findOrCreateConversation(account, contact,false); } else if (packet.getType() == MessagePacket.TYPE_GROUPCHAT) { String[] fromParts = fullJid.split("/"); if (fromParts.length != 2) { @@ -201,6 +201,9 @@ public class XmppConnectionService extends Service { public void getRoster(Account account, final OnRosterFetchedListener listener) { List contacts = databaseBackend.getContacts(account); + for(int i=0; i < contacts.size(); ++i) { + contacts.get(i).setAccount(account); + } if (listener != null) { listener.onRosterFetched(contacts); } @@ -339,27 +342,40 @@ public class XmppConnectionService extends Service { } public Conversation findOrCreateConversation(Account account, - Contact contact) { - // Log.d(LOGTAG,"was asked to find conversation for "+contact.getJid()); + Contact contact,boolean muc) { for (Conversation conv : this.getConversations()) { if ((conv.getAccount().equals(account)) && (conv.getContactJid().equals(contact.getJid()))) { - // Log.d(LOGTAG,"found one in memory"); return conv; } } Conversation conversation = databaseBackend.findConversation(account, contact.getJid()); if (conversation != null) { - Log.d("gultsch", "found one. unarchive it"); conversation.setStatus(Conversation.STATUS_AVAILABLE); conversation.setAccount(account); + if (muc) { + conversation.setMode(Conversation.MODE_MULTI); + if (account.getStatus()==Account.STATUS_ONLINE) { + joinMuc(account, conversation); + } + } else { + conversation.setMode(Conversation.MODE_SINGLE); + } this.databaseBackend.updateConversation(conversation); } else { - Log.d(LOGTAG, "didnt find one in archive. create new one"); - conversation = new Conversation(contact.getDisplayName(), - contact.getProfilePhoto(), account, contact.getJid(), - Conversation.MODE_SINGLE); + if (muc) { + conversation = new Conversation(contact.getDisplayName(), + contact.getProfilePhoto(), account, contact.getJid(), + Conversation.MODE_MULTI); + if (account.getStatus()==Account.STATUS_ONLINE) { + joinMuc(account, conversation); + } + } else { + conversation = new Conversation(contact.getDisplayName(), + contact.getProfilePhoto(), account, contact.getJid(), + Conversation.MODE_SINGLE); + } this.databaseBackend.createConversation(conversation); } this.conversations.add(conversation); @@ -443,20 +459,21 @@ public class XmppConnectionService extends Service { Conversation conversation = conversations.get(i); if ((conversation.getMode() == Conversation.MODE_MULTI) && (conversation.getAccount() == account)) { - String muc = conversation.getContactJid(); - Log.d(LOGTAG, - "join muc " + muc + " with account " + account.getJid()); - PresencePacket packet = new PresencePacket(); - packet.setAttribute("to", muc + "/" + account.getUsername()); - Element x = new Element("x"); - x.setAttribute("xmlns", "http://jabber.org/protocol/muc"); - packet.addChild(x); - connections.get(conversation.getAccount()).sendPresencePacket( - packet); - + joinMuc(account, conversation); } } } + + public void joinMuc(Account account, Conversation conversation) { + String muc = conversation.getContactJid(); + PresencePacket packet = new PresencePacket(); + packet.setAttribute("to", muc + "/" + account.getUsername()); + Element x = new Element("x"); + x.setAttribute("xmlns", "http://jabber.org/protocol/muc"); + packet.addChild(x); + connections.get(conversation.getAccount()).sendPresencePacket( + packet); + } public void disconnectMultiModeConversations() { diff --git a/src/de/gultsch/chat/ui/NewConversationActivity.java b/src/de/gultsch/chat/ui/NewConversationActivity.java index ec445e6c..c5f9695e 100644 --- a/src/de/gultsch/chat/ui/NewConversationActivity.java +++ b/src/de/gultsch/chat/ui/NewConversationActivity.java @@ -130,14 +130,16 @@ public class NewConversationActivity extends XmppActivity { @Override public View getView(int position, View view, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); + Contact contact = getItem(position); if (view == null) { view = (View) inflater.inflate(R.layout.contact, null); } ((TextView) view.findViewById(R.id.contact_display_name)) .setText(getItem(position).getDisplayName()); - ((TextView) view.findViewById(R.id.contact_jid)) - .setText(getItem(position).getJid()); + TextView contactJid = (TextView) view + .findViewById(R.id.contact_jid); + contactJid.setText(contact.getJid()); String profilePhoto = getItem(position).getProfilePhoto(); ImageView imageView = (ImageView) view .findViewById(R.id.contact_photo); @@ -158,39 +160,61 @@ public class NewConversationActivity extends XmppActivity { public void onItemClick(AdapterView arg0, final View view, int pos, long arg3) { final Contact clickedContact = aggregatedContacts.get(pos); - Log.d("gultsch", - "clicked on " + clickedContact.getDisplayName()); - - final List accounts = xmppConnectionService - .getAccounts(); - if (accounts.size() == 1) { - startConversation(clickedContact, accounts.get(0)); - } else { + + if ((clickedContact.getAccount()==null)&&(accounts.size()>1)) { String[] accountList = new String[accounts.size()]; for (int i = 0; i < accounts.size(); ++i) { - accountList[i] = accounts.get(i).getJid(); + accountList[i] = accounts.get(i).getJid(); } - AlertDialog.Builder builder = new AlertDialog.Builder( - activity); - builder.setTitle("Choose account"); - builder.setItems(accountList, new OnClickListener() { + AlertDialog.Builder accountChooser = new AlertDialog.Builder( + activity); + accountChooser.setTitle("Choose account"); + accountChooser.setItems(accountList, new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - Account account = accounts.get(which); - startConversation(clickedContact, account); - } + @Override + public void onClick(DialogInterface dialog, int which) { + clickedContact.setAccount(accounts.get(which)); + showIsMucDialogIfNeeded(clickedContact); + } }); - builder.create().show(); + accountChooser.create().show(); + } else { + clickedContact.setAccount(accounts.get(0)); + showIsMucDialogIfNeeded(clickedContact); } } }); } + + public void showIsMucDialogIfNeeded(final Contact clickedContact) { + if (clickedContact.couldBeMuc()) { + AlertDialog.Builder dialog = new AlertDialog.Builder(this); + dialog.setTitle("Multi User Conference"); + dialog.setMessage("Are you trying to join a conference?"); + dialog.setPositiveButton("Yes", new OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + startConversation(clickedContact, clickedContact.getAccount(),true); + } + }); + dialog.setNegativeButton("No", new OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + startConversation(clickedContact, clickedContact.getAccount(),false); + } + }); + dialog.create().show(); + } else { + startConversation(clickedContact, clickedContact.getAccount(),false); + } + } - public void startConversation(Contact contact, Account account) { + public void startConversation(Contact contact, Account account, boolean muc) { Conversation conversation = xmppConnectionService - .findOrCreateConversation(account, contact); + .findOrCreateConversation(account, contact, muc); Intent viewConversationIntent = new Intent(this, ConversationActivity.class); @@ -211,24 +235,25 @@ public class NewConversationActivity extends XmppActivity { } this.accounts = xmppConnectionService.getAccounts(); this.rosterContacts.clear(); - for(int i = 0; i < accounts.size(); ++i) { - if (accounts.get(i).getStatus()==Account.STATUS_ONLINE) { - xmppConnectionService.getRoster(accounts.get(i),new OnRosterFetchedListener() { - - @Override - public void onRosterFetched(List roster) { - rosterContacts.addAll(roster); - runOnUiThread(new Runnable() { - + for (int i = 0; i < accounts.size(); ++i) { + if (accounts.get(i).getStatus() == Account.STATUS_ONLINE) { + xmppConnectionService.getRoster(accounts.get(i), + new OnRosterFetchedListener() { + @Override - public void run() { - updateAggregatedContacts(); + public void onRosterFetched(List roster) { + rosterContacts.addAll(roster); + runOnUiThread(new Runnable() { + + @Override + public void run() { + updateAggregatedContacts(); + } + }); + } }); - - } - }); - } + } } } @@ -269,26 +294,27 @@ public class NewConversationActivity extends XmppActivity { this.accounts = xmppConnectionService.getAccounts(); this.rosterContacts.clear(); for (int i = 0; i < accounts.size(); ++i) { - if (accounts.get(i).getStatus()==Account.STATUS_ONLINE) { - xmppConnectionService.updateRoster(accounts.get(i), - new OnRosterFetchedListener() { - - @Override - public void onRosterFetched(final List roster) { - runOnUiThread(new Runnable() { - - @Override - public void run() { - rosterContacts.addAll(roster); - progress.setVisibility(View.GONE); - searchBar.setVisibility(View.VISIBLE); - contactList.setVisibility(View.VISIBLE); - contactList.setVisibility(View.VISIBLE); - updateAggregatedContacts(); - } - }); - } - }); + if (accounts.get(i).getStatus() == Account.STATUS_ONLINE) { + xmppConnectionService.updateRoster(accounts.get(i), + new OnRosterFetchedListener() { + + @Override + public void onRosterFetched( + final List roster) { + runOnUiThread(new Runnable() { + + @Override + public void run() { + rosterContacts.addAll(roster); + progress.setVisibility(View.GONE); + searchBar.setVisibility(View.VISIBLE); + contactList.setVisibility(View.VISIBLE); + contactList.setVisibility(View.VISIBLE); + updateAggregatedContacts(); + } + }); + } + }); } } } diff --git a/src/de/gultsch/chat/xmpp/XmppConnection.java b/src/de/gultsch/chat/xmpp/XmppConnection.java index d7443fc4..772c2430 100644 --- a/src/de/gultsch/chat/xmpp/XmppConnection.java +++ b/src/de/gultsch/chat/xmpp/XmppConnection.java @@ -321,7 +321,7 @@ public class XmppConnection implements Runnable { } private void sendStartStream() { - Tag stream = Tag.start("stream"); + Tag stream = Tag.start("stream:stream"); stream.setAttribute("from", account.getJid()); stream.setAttribute("to", account.getServer()); stream.setAttribute("version", "1.0"); -- cgit v1.2.3