9 import org.jivesoftware.smack.Chat; |
9 import org.jivesoftware.smack.Chat; |
10 import org.jivesoftware.smack.ChatManager; |
10 import org.jivesoftware.smack.ChatManager; |
11 import org.jivesoftware.smack.ChatManagerListener; |
11 import org.jivesoftware.smack.ChatManagerListener; |
12 import org.jivesoftware.smack.MessageListener; |
12 import org.jivesoftware.smack.MessageListener; |
13 import org.jivesoftware.smack.packet.Message; |
13 import org.jivesoftware.smack.packet.Message; |
|
14 import org.jivesoftware.smack.util.StringUtils; |
14 import org.jivesoftware.smackx.ChatState; |
15 import org.jivesoftware.smackx.ChatState; |
15 import org.jivesoftware.smackx.ChatStateListener; |
16 import org.jivesoftware.smackx.ChatStateListener; |
16 |
17 |
17 import android.R; |
18 import android.R; |
18 import android.app.Notification; |
19 import android.app.Notification; |
62 * @param listener listener to use for chat events on this chat session |
63 * @param listener listener to use for chat events on this chat session |
63 * @return the chat session |
64 * @return the chat session |
64 */ |
65 */ |
65 public IChat createChat(String jid, IMessageListener listener) { |
66 public IChat createChat(String jid, IMessageListener listener) { |
66 mRemoteMessageListeners.register(listener); |
67 mRemoteMessageListeners.register(listener); |
67 if (mChats.containsKey(jid)) { |
68 String key = StringUtils.parseBareAddress(jid); |
68 return mChats.get(jid); |
69 if (mChats.containsKey(key)) { |
|
70 return mChats.get(key); |
69 } |
71 } |
70 // create the chat. the adaptee will be add automatically in the map |
72 // create the chat. the adaptee will be add automatically in the map |
71 mAdaptee.createChat(jid, mChatListener); |
73 mAdaptee.createChat(key, mChatListener); |
72 return mChats.get(jid); |
74 return mChats.get(key); |
73 } |
75 } |
74 |
76 |
75 /** |
77 /** |
76 * Create a chat session. |
78 * Create a chat session. |
77 * @param contact the contact you want to chat with |
79 * @param contact the contact you want to chat with |
96 */ |
98 */ |
97 @Override |
99 @Override |
98 public void removeChatCreationListener(IChatManagerListener listener) throws RemoteException { |
100 public void removeChatCreationListener(IChatManagerListener listener) throws RemoteException { |
99 mRemoteChatCreationListeners.unregister(listener); |
101 mRemoteChatCreationListeners.unregister(listener); |
100 } |
102 } |
101 |
103 |
102 /** |
104 /** |
103 * {@inheritDoc} |
105 * {@inheritDoc} |
104 */ |
106 */ |
105 @Override |
107 @Override |
106 public void destroyChat(IChat chat) throws RemoteException { |
108 public void destroyChat(IChat chat) throws RemoteException { |
107 //TODO gerer les resources egalement |
109 // TODO gerer les resources egalement |
108 IChat c=mChats.remove(chat.getParticipant().getJID()); |
110 IChat c = mChats.remove(chat.getParticipant().getJID()); |
109 if (c==null) |
111 if (c == null) |
110 Log.w(TAG, "CA devrait pas 1!!"); |
112 Log.w(TAG, "CA devrait pas 1!!"); |
111 } |
113 } |
112 |
114 |
113 private IChat getChat(Chat chat) { |
115 private IChat getChat(Chat chat) { |
114 if (mChats.containsKey(chat.getParticipant())) { |
116 String key = StringUtils.parseBareAddress(chat.getParticipant()); |
115 return mChats.get(chat.getParticipant()); |
117 if (mChats.containsKey(key)) { |
|
118 return mChats.get(key); |
116 } |
119 } |
117 IChat res = new ChatAdapter(chat); |
120 IChat res = new ChatAdapter(chat); |
118 mChats.put(chat.getParticipant(), res); |
121 mChats.put(key, res); |
119 return res; |
122 return res; |
120 } |
123 } |
121 |
124 |
122 /** |
125 /** |
123 * A listener for all the chat creation event that happens on the connection. |
126 * A listener for all the chat creation event that happens on the connection. |
158 |
161 |
159 private void notifyNewChat(IChat chat) { |
162 private void notifyNewChat(IChat chat) { |
160 try { |
163 try { |
161 String text = chat.getParticipant().getJID(); |
164 String text = chat.getParticipant().getJID(); |
162 Notification notif = new Notification(com.beem.project.beem.R.drawable.logo, text, System |
165 Notification notif = new Notification(com.beem.project.beem.R.drawable.logo, text, System |
163 .currentTimeMillis()); |
166 .currentTimeMillis()); |
164 notif.defaults = Notification.DEFAULT_ALL; |
167 notif.defaults = Notification.DEFAULT_ALL; |
165 notif.flags = Notification.FLAG_AUTO_CANCEL; |
168 notif.flags = Notification.FLAG_AUTO_CANCEL; |
166 Intent intent = new Intent(mService, SendIM.class); |
169 Intent intent = new Intent(mService, SendIM.class); |
167 // TODO use prefix for name |
170 // TODO use prefix for name |
168 intent.putExtra("contact", chat.getParticipant()); |
171 intent.putExtra("contact", chat.getParticipant()); |
177 } |
180 } |
178 |
181 |
179 @Override |
182 @Override |
180 public void processMessage(Chat chat, Message message) { |
183 public void processMessage(Chat chat, Message message) { |
181 IChat newchat = getChat(chat); |
184 IChat newchat = getChat(chat); |
182 final int n = mRemoteMessageListeners.beginBroadcast(); |
185 try { |
183 for (int i = 0; i < n; i++) { |
186 newchat.addToLastMessages(message.getBody()); |
184 IMessageListener listener = mRemoteMessageListeners.getBroadcastItem(i); |
187 final int n = mRemoteMessageListeners.beginBroadcast(); |
185 try { |
188 for (int i = 0; i < n; i++) { |
|
189 IMessageListener listener = mRemoteMessageListeners.getBroadcastItem(i); |
186 listener.processMessage(newchat, new com.beem.project.beem.service.Message(message)); |
190 listener.processMessage(newchat, new com.beem.project.beem.service.Message(message)); |
187 // listener.chatCreated(newchat, locally); |
191 |
188 } catch (RemoteException e) { |
|
189 // The RemoteCallbackList will take care of removing the |
|
190 // dead listeners. |
|
191 Log.w(TAG, "Error while triggering remote connection listeners", e); |
|
192 } |
192 } |
193 } |
193 mRemoteMessageListeners.finishBroadcast(); |
194 mRemoteMessageListeners.finishBroadcast(); |
194 } catch (RemoteException e) { |
|
195 // The RemoteCallbackList will take care of removing the |
|
196 // dead listeners. |
|
197 Log.w(TAG, "Error while triggering remote connection listeners", e); |
|
198 } |
195 } |
199 } |
196 |
200 |
197 @Override |
201 @Override |
198 public void stateChanged(Chat chat, ChatState state) { |
202 public void stateChanged(Chat chat, ChatState state) { |
199 try { |
203 try { |