示例#1
0
 /** Register a "pong" timeout on the connection. */
 private void registerPongTimeout(long wait_time, String id) {
   mPingID = id;
   mPingTimestamp = System.currentTimeMillis();
   debugLog(String.format("Ping: registering timeout for %s: %1.3fs", id, wait_time / 1000.));
   mAlarmManager.set(
       AlarmManager.RTC_WAKEUP,
       System.currentTimeMillis() + wait_time,
       mPongTimeoutAlarmPendIntent);
 }
示例#2
0
 private void gotServerPong(String pongID) {
   long latency = System.currentTimeMillis() - mPingTimestamp;
   if (pongID != null && pongID.equals(mPingID))
     Log.i(TAG, String.format("Ping: server latency %1.3fs", latency / 1000.));
   else Log.i(TAG, String.format("Ping: server latency %1.3fs (estimated)", latency / 1000.));
   mPingID = null;
   mAlarmManager.cancel(mPongTimeoutAlarmPendIntent);
 }
示例#3
0
  public static void sendOfflineMessage(ContentResolver cr, String toJID, String message) {
    ContentValues values = new ContentValues();
    values.put(ChatConstants.DIRECTION, ChatConstants.OUTGOING);
    values.put(ChatConstants.JID, toJID);
    values.put(ChatConstants.MESSAGE, message);
    values.put(ChatConstants.DELIVERY_STATUS, ChatConstants.DS_NEW);
    values.put(ChatConstants.DATE, System.currentTimeMillis());

    cr.insert(ChatProvider.CONTENT_URI, values);
  }
示例#4
0
 public void sendMessage(String toJID, String message) {
   final Message newMessage = new Message(toJID, Message.Type.chat);
   newMessage.setBody(message);
   newMessage.addExtension(new DeliveryReceiptRequest());
   if (isAuthenticated()) {
     addChatMessageToDB(
         ChatConstants.OUTGOING,
         toJID,
         message,
         ChatConstants.DS_SENT_OR_READ,
         System.currentTimeMillis(),
         newMessage.getPacketID());
     mXMPPConnection.sendPacket(newMessage);
   } else {
     // send offline -> store to DB
     addChatMessageToDB(
         ChatConstants.OUTGOING,
         toJID,
         message,
         ChatConstants.DS_NEW,
         System.currentTimeMillis(),
         newMessage.getPacketID());
   }
 }
示例#5
0
  /**
   * 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);
  }