private void registerPresenceListener() { // do not register multiple packet listeners if (mPresenceListener != null) mXMPPConnection.removePacketListener(mPresenceListener); mPresenceListener = new PacketListener() { public void processPacket(Packet packet) { try { Presence p = (Presence) packet; switch (p.getType()) { case subscribe: handleIncomingSubscribe(p); break; case unsubscribe: break; } } catch (Exception e) { // SMACK silently discards exceptions dropped from processPacket :( Log.e(TAG, "failed to process presence:"); e.printStackTrace(); } } }; mXMPPConnection.addPacketListener(mPresenceListener, new PacketTypeFilter(Presence.class)); }
/** * Registers a smack packet listener for IQ packets, intended to recognize "pongs" with a packet * id matching the last "ping" sent to the server. * * <p>Also sets up the AlarmManager Timer plus necessary intents. */ private void registerPongListener() { // reset ping expectation on new connection mPingID = null; if (mPongListener != null) mXMPPConnection.removePacketListener(mPongListener); mPongListener = new PacketListener() { @Override public void processPacket(Packet packet) { if (packet == null) return; gotServerPong(packet.getPacketID()); } }; mXMPPConnection.addPacketListener(mPongListener, new PacketTypeFilter(IQ.class)); mPingAlarmPendIntent = PendingIntent.getBroadcast( mService.getApplicationContext(), 0, mPingAlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); mPongTimeoutAlarmPendIntent = PendingIntent.getBroadcast( mService.getApplicationContext(), 0, mPongTimeoutAlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); mAlarmManager.setInexactRepeating( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + AlarmManager.INTERVAL_FIFTEEN_MINUTES, AlarmManager.INTERVAL_FIFTEEN_MINUTES, mPingAlarmPendIntent); }
private void registerMessageListener() { // do not register multiple packet listeners if (mPacketListener != null) mXMPPConnection.removePacketListener(mPacketListener); PacketTypeFilter filter = new PacketTypeFilter(Message.class); mPacketListener = new PacketListener() { public void processPacket(Packet packet) { try { if (packet instanceof Message) { Message msg = (Message) packet; String fromJID = getBareJID(msg.getFrom()); int direction = ChatConstants.INCOMING; Carbon cc = CarbonManager.getCarbon(msg); // extract timestamp long ts; DelayInfo timestamp = (DelayInfo) msg.getExtension("delay", "urn:xmpp:delay"); if (timestamp == null) timestamp = (DelayInfo) msg.getExtension("x", "jabber:x:delay"); if (cc != null) // Carbon timestamp overrides packet timestamp timestamp = cc.getForwarded().getDelayInfo(); if (timestamp != null) ts = timestamp.getStamp().getTime(); else ts = System.currentTimeMillis(); // try to extract a carbon if (cc != null) { Log.d(TAG, "carbon: " + cc.toXML()); msg = (Message) cc.getForwarded().getForwardedPacket(); // outgoing carbon: fromJID is actually chat peer's JID if (cc.getDirection() == Carbon.Direction.sent) { fromJID = getBareJID(msg.getTo()); direction = ChatConstants.OUTGOING; } else { fromJID = getBareJID(msg.getFrom()); // hook off carbonated delivery receipts DeliveryReceipt dr = (DeliveryReceipt) msg.getExtension(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE); if (dr != null) { Log.d(TAG, "got CC'ed delivery receipt for " + dr.getId()); changeMessageDeliveryStatus(dr.getId(), ChatConstants.DS_ACKED); } } } String chatMessage = msg.getBody(); // display error inline if (msg.getType() == Message.Type.error) { if (changeMessageDeliveryStatus(msg.getPacketID(), ChatConstants.DS_FAILED)) mServiceCallBack.messageError(fromJID, msg.getError().toString(), (cc != null)); return; // we do not want to add errors as "incoming messages" } // ignore empty messages if (chatMessage == null) { Log.d(TAG, "empty message."); return; } // carbons are old. all others are new int is_new = (cc == null) ? ChatConstants.DS_NEW : ChatConstants.DS_SENT_OR_READ; if (msg.getType() == Message.Type.error) is_new = ChatConstants.DS_FAILED; addChatMessageToDB(direction, fromJID, chatMessage, is_new, ts, msg.getPacketID()); if (direction == ChatConstants.INCOMING) mServiceCallBack.newMessage(fromJID, chatMessage, (cc != null)); } } catch (Exception e) { // SMACK silently discards exceptions dropped from processPacket :( Log.e(TAG, "failed to process packet:"); e.printStackTrace(); } } }; mXMPPConnection.addPacketListener(mPacketListener, filter); }