Example #1
0
  private void registerPongListener() {
    pingID = null;

    if (pongListener != null) extXMPPConnection.removePacketListener(pongListener);

    pongListener =
        new PacketListener() {

          @Override
          public void processPacket(Packet packet) {
            if (packet == null) return;

            gotServerPong(packet.getPacketID());
          }
        };

    extXMPPConnection.addPacketListener(pongListener, new PacketTypeFilter(IQ.class));
    pingAlarmPendIntent =
        PendingIntent.getBroadcast(
            service.getApplicationContext(), 0, intentPingAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
    timeoutAlarmPendIntent =
        PendingIntent.getBroadcast(
            service.getApplicationContext(),
            0,
            intentTimeoutAlarm,
            PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.setInexactRepeating(
        AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis() + AlarmManager.INTERVAL_FIFTEEN_MINUTES,
        AlarmManager.INTERVAL_FIFTEEN_MINUTES,
        pingAlarmPendIntent);
  }
Example #2
0
  public void unRegisterCallback() {
    try {
      extXMPPConnection.getRoster().removeRosterListener(rosterListener);
      extXMPPConnection.removePacketListener(packetListener);
      extXMPPConnection.removePacketListener(presenceListener);

      extXMPPConnection.removePacketListener(pongListener);
      unregisterPongListener();
    } catch (Exception e) {
      e.printStackTrace();
    }
    requestConnectionState(ConnectionState.OFFLINE);
    setStatusOffline();
    service.unregisterReceiver(receiverPingAlarm);
    service.unregisterReceiver(receiverTimeoutAlarm);
    this.serviceCallback = null;
  }
Example #3
0
  private void registerPresenceListener() {

    if (presenceListener != null) extXMPPConnection.removePacketListener(presenceListener);

    presenceListener =
        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) {
              e.printStackTrace();
            }
          }
        };

    extXMPPConnection.addPacketListener(presenceListener, new PacketTypeFilter(Presence.class));
  }
Example #4
0
  private void registerMessageListener() {

    if (packetListener != null) extXMPPConnection.removePacketListener(packetListener);

    PacketTypeFilter filter = new PacketTypeFilter(Message.class);

    packetListener =
        new PacketListener() {
          public void processPacket(Packet packet) {
            try {
              if (packet instanceof Message) {
                Message message = (Message) packet;

                String fromJID = getBareJID(message.getFrom());
                int direction = ChatConstants.INCOMING;
                Carbon carbon = CarbonManager.getCarbon(message);

                // extract timestamp
                long ts;
                DelayInfo timestamp =
                    (DelayInfo) message.getExtension(Constant.DELAY, Constant.URN_XMPP_DELAY);
                if (timestamp == null)
                  timestamp = (DelayInfo) message.getExtension(Constant.X, Constant.JABBER_X_DELAY);
                if (carbon != null) timestamp = carbon.getForwarded().getDelayInfo();
                if (timestamp != null) ts = timestamp.getStamp().getTime();
                else ts = System.currentTimeMillis();
                if (carbon != null) {

                  message = (Message) carbon.getForwarded().getForwardedPacket();

                  if (carbon.getDirection() == Carbon.Direction.sent) {
                    fromJID = getBareJID(message.getTo());
                    direction = ChatConstants.OUTGOING;
                  } else {
                    fromJID = getBareJID(message.getFrom());
                    DeliveryReceipt dr =
                        (DeliveryReceipt)
                            message.getExtension(
                                DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE);
                    if (dr != null) {

                      changeMessageDeliveryStatus(dr.getId(), ChatConstants.DS_ACKED);
                    }
                  }
                }

                String chatMessage = message.getBody();
                if (message.getType() == Message.Type.error) {
                  if (changeMessageDeliveryStatus(message.getPacketID(), ChatConstants.DS_FAILED))
                    serviceCallback.messageError(
                        fromJID, message.getError().toString(), (carbon != null));
                  return;
                }

                if (chatMessage == null) {
                  return;
                }

                int is_new =
                    (carbon == null) ? ChatConstants.DS_NEW : ChatConstants.DS_SENT_OR_READ;
                if (message.getType() == Message.Type.error) is_new = ChatConstants.DS_FAILED;

                addChatMessageToDB(
                    direction, fromJID, chatMessage, is_new, ts, message.getPacketID());
                if (direction == ChatConstants.INCOMING)
                  serviceCallback.newMessage(fromJID, chatMessage, (carbon != null));
              }
            } catch (Exception e) {

              e.printStackTrace();
            }
          }
        };

    extXMPPConnection.addPacketListener(packetListener, filter);
  }