1 20 21 package org.jivesoftware.smackx; 22 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import java.util.*; 26 27 import org.jivesoftware.smack.*; 28 import org.jivesoftware.smack.filter.*; 29 import org.jivesoftware.smack.packet.*; 30 import org.jivesoftware.smackx.packet.*; 31 32 39 public class MessageEventManager { 40 41 private List messageEventNotificationListeners = new ArrayList(); 42 private List messageEventRequestListeners = new ArrayList(); 43 44 private XMPPConnection con; 45 46 private PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:event"); 47 private PacketListener packetListener; 48 49 54 public MessageEventManager(XMPPConnection con) { 55 this.con = con; 56 init(); 57 } 58 59 70 public static void addNotificationsRequests(Message message, boolean offline, 71 boolean delivered, boolean displayed, boolean composing) 72 { 73 MessageEvent messageEvent = new MessageEvent(); 75 messageEvent.setOffline(offline); 76 messageEvent.setDelivered(delivered); 77 messageEvent.setDisplayed(displayed); 78 messageEvent.setComposing(composing); 79 message.addExtension(messageEvent); 80 } 81 82 88 public void addMessageEventRequestListener(MessageEventRequestListener messageEventRequestListener) { 89 synchronized (messageEventRequestListeners) { 90 if (!messageEventRequestListeners.contains(messageEventRequestListener)) { 91 messageEventRequestListeners.add(messageEventRequestListener); 92 } 93 } 94 } 95 96 102 public void removeMessageEventRequestListener(MessageEventRequestListener messageEventRequestListener) { 103 synchronized (messageEventRequestListeners) { 104 messageEventRequestListeners.remove(messageEventRequestListener); 105 } 106 } 107 108 114 public void addMessageEventNotificationListener(MessageEventNotificationListener messageEventNotificationListener) { 115 synchronized (messageEventNotificationListeners) { 116 if (!messageEventNotificationListeners.contains(messageEventNotificationListener)) { 117 messageEventNotificationListeners.add(messageEventNotificationListener); 118 } 119 } 120 } 121 122 128 public void removeMessageEventNotificationListener(MessageEventNotificationListener messageEventNotificationListener) { 129 synchronized (messageEventNotificationListeners) { 130 messageEventNotificationListeners.remove(messageEventNotificationListener); 131 } 132 } 133 134 137 private void fireMessageEventRequestListeners( 138 String from, 139 String packetID, 140 String methodName) { 141 MessageEventRequestListener[] listeners = null; 142 Method method; 143 synchronized (messageEventRequestListeners) { 144 listeners = new MessageEventRequestListener[messageEventRequestListeners.size()]; 145 messageEventRequestListeners.toArray(listeners); 146 } 147 try { 148 method = 149 MessageEventRequestListener.class.getDeclaredMethod( 150 methodName, 151 new Class [] { String .class, String .class, MessageEventManager.class }); 152 for (int i = 0; i < listeners.length; i++) { 153 method.invoke(listeners[i], new Object [] { from, packetID, this }); 154 } 155 } catch (NoSuchMethodException e) { 156 e.printStackTrace(); 157 } catch (InvocationTargetException e) { 158 e.printStackTrace(); 159 } catch (IllegalAccessException e) { 160 e.printStackTrace(); 161 } 162 } 163 164 167 private void fireMessageEventNotificationListeners( 168 String from, 169 String packetID, 170 String methodName) { 171 MessageEventNotificationListener[] listeners = null; 172 Method method; 173 synchronized (messageEventNotificationListeners) { 174 listeners = 175 new MessageEventNotificationListener[messageEventNotificationListeners.size()]; 176 messageEventNotificationListeners.toArray(listeners); 177 } 178 try { 179 method = 180 MessageEventNotificationListener.class.getDeclaredMethod( 181 methodName, 182 new Class [] { String .class, String .class }); 183 for (int i = 0; i < listeners.length; i++) { 184 method.invoke(listeners[i], new Object [] { from, packetID }); 185 } 186 } catch (NoSuchMethodException e) { 187 e.printStackTrace(); 188 } catch (InvocationTargetException e) { 189 e.printStackTrace(); 190 } catch (IllegalAccessException e) { 191 e.printStackTrace(); 192 } 193 } 194 195 private void init() { 196 packetListener = new PacketListener() { 198 public void processPacket(Packet packet) { 199 Message message = (Message) packet; 200 MessageEvent messageEvent = 201 (MessageEvent) message.getExtension("x", "jabber:x:event"); 202 if (messageEvent.isMessageEventRequest()) { 203 for (Iterator it = messageEvent.getEventTypes(); it.hasNext();) 205 fireMessageEventRequestListeners( 206 message.getFrom(), 207 message.getPacketID(), 208 ((String ) it.next()).concat("NotificationRequested")); 209 } else 210 for (Iterator it = messageEvent.getEventTypes(); it.hasNext();) 212 fireMessageEventNotificationListeners( 213 message.getFrom(), 214 messageEvent.getPacketID(), 215 ((String ) it.next()).concat("Notification")); 216 217 }; 218 219 }; 220 con.addPacketListener(packetListener, packetFilter); 221 } 222 223 229 public void sendDeliveredNotification(String to, String packetID) { 230 Message msg = new Message(to); 232 MessageEvent messageEvent = new MessageEvent(); 234 messageEvent.setDelivered(true); 235 messageEvent.setPacketID(packetID); 236 msg.addExtension(messageEvent); 237 con.sendPacket(msg); 239 } 240 241 247 public void sendDisplayedNotification(String to, String packetID) { 248 Message msg = new Message(to); 250 MessageEvent messageEvent = new MessageEvent(); 252 messageEvent.setDisplayed(true); 253 messageEvent.setPacketID(packetID); 254 msg.addExtension(messageEvent); 255 con.sendPacket(msg); 257 } 258 259 265 public void sendComposingNotification(String to, String packetID) { 266 Message msg = new Message(to); 268 MessageEvent messageEvent = new MessageEvent(); 270 messageEvent.setComposing(true); 271 messageEvent.setPacketID(packetID); 272 msg.addExtension(messageEvent); 273 con.sendPacket(msg); 275 } 276 277 283 public void sendCancelledNotification(String to, String packetID) { 284 Message msg = new Message(to); 286 MessageEvent messageEvent = new MessageEvent(); 288 messageEvent.setCancelled(true); 289 messageEvent.setPacketID(packetID); 290 msg.addExtension(messageEvent); 291 con.sendPacket(msg); 293 } 294 295 public void destroy() { 296 if (con != null) { 297 con.removePacketListener(packetListener); 298 } 299 } 300 301 public void finalize() { 302 destroy(); 303 } 304 } | Popular Tags |