Bug #272 ยป open_chats.patch
| src/com/beem/project/beem/service/BeemChatManager.java Thu Jun 10 20:08:03 2010 +0200 โ src/com/beem/project/beem/service/BeemChatManager.java Mon Jun 14 11:28:07 2010 -0500 | ||
|---|---|---|
| 44 | 44 | package com.beem.project.beem.service; | 
| 45 | 45 | |
| 46 | 46 | import java.util.ArrayList; | 
| 47 | import java.util.Collections; | |
| 47 | 48 | import java.util.HashMap; | 
| 48 | 49 | import java.util.List; | 
| 49 | 50 | import java.util.Map; | 
| ... | ... | |
| 193 | 194 | IRoster mRoster = mService.getBind().getRoster(); | 
| 194 | 195 | |
| 195 | 196 | 	for (ChatAdapter chat : mChats.values()) { | 
| 196 | 	    if (chat.getMessages().size() > 0) { | |
| 197 | if (chat.getMessageCount(Message.MSG_TYPE_CHAT) > 0 || | |
| 198 | 	        chat.getMessageCount(Message.MSG_TYPE_GROUP_CHAT) > 0) { | |
| 197 | 199 | Contact t = mRoster.getContact(chat.getParticipant().getJID()); | 
| 198 | 200 | if (t == null) | 
| 199 | 201 | t = new Contact(chat.getParticipant().getJID()); | 
| 200 | 202 | openedChats.add(t); | 
| 201 | 203 | } | 
| 202 | 204 | } | 
| 205 | Collections.sort(openedChats, new Contact.NameComparator()); | |
| 203 | 206 | return openedChats; | 
| 204 | 207 | } | 
| 205 | 208 | |
| src/com/beem/project/beem/service/ChatAdapter.java Thu Jun 10 20:08:03 2010 +0200 โ src/com/beem/project/beem/service/ChatAdapter.java Mon Jun 14 11:28:07 2010 -0500 | ||
|---|---|---|
| 70 | 70 | private final Chat mAdaptee; | 
| 71 | 71 | private final Contact mParticipant; | 
| 72 | 72 | private String mState; | 
| 73 | private int[] mCounts; | |
| 73 | 74 | private boolean mIsOpen; | 
| 74 | 75 | private final List<Message> mMessages; | 
| 75 | 76 | private final RemoteCallbackList<IMessageListener> mRemoteListeners = new RemoteCallbackList<IMessageListener>(); | 
| ... | ... | |
| 84 | 85 | mParticipant = new Contact(chat.getParticipant()); | 
| 85 | 86 | mMessages = new LinkedList<Message>(); | 
| 86 | 87 | mAdaptee.addMessageListener(mMsgListener); | 
| 88 | mCounts = new int[4]; | |
| 87 | 89 | } | 
| 88 | 90 | |
| 89 | 91 | /** | 
| ... | ... | |
| 111 | 113 | 	try { | 
| 112 | 114 | mAdaptee.sendMessage(send); | 
| 113 | 115 | mMessages.add(message); | 
| 116 | updateMessageCount(message, false); | |
| 114 | 117 | 	} catch (XMPPException e) { | 
| 115 | 118 | // TODO Auto-generated catch block | 
| 116 | 119 | e.printStackTrace(); | 
| ... | ... | |
| 184 | 187 | return Collections.unmodifiableList(mMessages); | 
| 185 | 188 | } | 
| 186 | 189 | |
| 190 |     public int getMessageCount(int type) { | |
| 191 | int count = 0; | |
| 192 | 	switch (type) { | |
| 193 | case Message.MSG_TYPE_NORMAL: | |
| 194 | count = mCounts[0]; | |
| 195 | break; | |
| 196 | case Message.MSG_TYPE_CHAT: | |
| 197 | count = mCounts[1]; | |
| 198 | break; | |
| 199 | case Message.MSG_TYPE_GROUP_CHAT: | |
| 200 | count = mCounts[2]; | |
| 201 | break; | |
| 202 | case Message.MSG_TYPE_ERROR: | |
| 203 | count = mCounts[3]; | |
| 204 | break; | |
| 205 | default: | |
| 206 | } | |
| 207 | return count; | |
| 208 | } | |
| 209 | ||
| 187 | 210 | /** | 
| 188 | 211 | * Add a message in the chat history. | 
| 189 | 212 | * @param msg the message to add | 
| 190 | 213 | */ | 
| 191 | 214 |     void addMessage(Message msg) { | 
| 192 | 215 | if (mMessages.size() == HISTORY_MAX_SIZE) | 
| 193 | 	    mMessages.remove(0); | |
| 216 | 	    updateMessageCount(mMessages.remove(0), true); | |
| 194 | 217 | mMessages.add(msg); | 
| 218 | updateMessageCount(msg, false); | |
| 219 | } | |
| 220 | ||
| 221 | /** | |
| 222 | * Increment or decrement the message counts in the chat history. | |
| 223 | * @param msg the message to update counts | |
| 224 | */ | |
| 225 |     private void updateMessageCount(Message msg, Boolean down) { | |
| 226 | int val; | |
| 227 | ||
| 228 | if (down) | |
| 229 | val = -1; | |
| 230 | else | |
| 231 | val = 1; | |
| 232 | ||
| 233 | 	switch (msg.getType()) { | |
| 234 | case Message.MSG_TYPE_NORMAL: | |
| 235 | mCounts[0] += val; | |
| 236 | break; | |
| 237 | case Message.MSG_TYPE_CHAT: | |
| 238 | mCounts[1] += val; | |
| 239 | break; | |
| 240 | case Message.MSG_TYPE_GROUP_CHAT: | |
| 241 | mCounts[2] += val; | |
| 242 | break; | |
| 243 | case Message.MSG_TYPE_ERROR: | |
| 244 | mCounts[3] += val; | |
| 245 | break; | |
| 246 | default: | |
| 247 | } | |
| 195 | 248 | } | 
| 196 | 249 | |
| 197 | 250 | /** | 
| src/com/beem/project/beem/service/Contact.java Thu Jun 10 20:08:03 2010 +0200 โ src/com/beem/project/beem/service/Contact.java Mon Jun 14 11:28:07 2010 -0500 | ||
|---|---|---|
| 45 | 45 | |
| 46 | 46 | import java.util.ArrayList; | 
| 47 | 47 | import java.util.Collection; | 
| 48 | import java.util.Comparator; | |
| 48 | 49 | import java.util.List; | 
| 49 | 50 | |
| 50 | 51 | import org.jivesoftware.smack.RosterGroup; | 
| ... | ... | |
| 422 | 423 | return mJID.hashCode(); | 
| 423 | 424 | } | 
| 424 | 425 | |
| 426 |     static public class NameComparator implements Comparator { | |
| 427 |  | |
| 428 |         public boolean equals(Object a, Object b) { | |
| 429 | String sa, sb; | |
| 430 | sa = ((Contact)a).getName(); | |
| 431 | sb = ((Contact)b).getName(); | |
| 432 | return sa.equalsIgnoreCase(sb); | |
| 433 | } | |
| 434 |  | |
| 435 |         public int compare(Object a, Object b) { | |
| 436 | String sa, sb; | |
| 437 | sa = ((Contact)a).getName(); | |
| 438 | sb = ((Contact)b).getName(); | |
| 439 | return sa.compareToIgnoreCase(sb); | |
| 440 | } | |
| 441 | } | |
| 425 | 442 | } |