Пример #1
0
  public void sendOfflineMessages() {
    Cursor cursor =
        contentResolver.query(
            ChatProvider.CONTENT_URI, SEND_OFFLINE_PROJECTION, SEND_OFFLINE_SELECTION, null, null);
    final int ID = cursor.getColumnIndexOrThrow(ChatConstants._ID);
    final int JABBER_ID = cursor.getColumnIndexOrThrow(ChatConstants.JID);
    final int MESSAGE = cursor.getColumnIndexOrThrow(ChatConstants.MESSAGE);
    final int DATE = cursor.getColumnIndexOrThrow(ChatConstants.DATE);
    final int PACKET_ID = cursor.getColumnIndexOrThrow(ChatConstants.PACKET_ID);

    ContentValues mark_sent = new ContentValues();
    mark_sent.put(ChatConstants.DELIVERY_STATUS, ChatConstants.DS_SENT_OR_READ);
    while (cursor.moveToNext()) {
      int id = cursor.getInt(ID);
      String toJID = cursor.getString(JABBER_ID);
      String message = cursor.getString(MESSAGE);
      String packetID = cursor.getString(PACKET_ID);
      long dateTs = cursor.getLong(DATE);
      final Message newMsg = new Message(toJID, Message.Type.chat);
      newMsg.setBody(message);
      DelayInformation delay = new DelayInformation(new Date(dateTs));
      newMsg.addExtension(delay);
      newMsg.addExtension(new DelayInfo(delay));
      newMsg.addExtension(new DeliveryReceiptRequest());
      if ((packetID != null) && (packetID.length() > 0)) {
        newMsg.setPacketID(packetID);
      } else {
        packetID = newMsg.getPacketID();
        mark_sent.put(ChatConstants.PACKET_ID, packetID);
      }
      Uri rowuri =
          Uri.parse(
              Constant.CONTENT_PATH
                  + ChatProvider.AUTHORITY
                  + "/"
                  + ChatProvider.TABLE_NAME
                  + "/"
                  + id);
      contentResolver.update(rowuri, mark_sent, null, null);
      extXMPPConnection.sendPacket(newMsg);
    }
    cursor.close();
  }
Пример #2
0
 public void sendOfflineMessages() {
   Cursor cursor =
       mContentResolver.query(
           ChatProvider.CONTENT_URI, SEND_OFFLINE_PROJECTION, SEND_OFFLINE_SELECTION, null, null);
   final int _ID_COL = cursor.getColumnIndexOrThrow(ChatConstants._ID);
   final int JID_COL = cursor.getColumnIndexOrThrow(ChatConstants.JID);
   final int MSG_COL = cursor.getColumnIndexOrThrow(ChatConstants.MESSAGE);
   final int TS_COL = cursor.getColumnIndexOrThrow(ChatConstants.DATE);
   final int PACKETID_COL = cursor.getColumnIndexOrThrow(ChatConstants.PACKET_ID);
   ContentValues mark_sent = new ContentValues();
   mark_sent.put(ChatConstants.DELIVERY_STATUS, ChatConstants.DS_SENT_OR_READ);
   while (cursor.moveToNext()) {
     int _id = cursor.getInt(_ID_COL);
     String toJID = cursor.getString(JID_COL);
     String message = cursor.getString(MSG_COL);
     String packetID = cursor.getString(PACKETID_COL);
     long ts = cursor.getLong(TS_COL);
     Log.d(TAG, "sendOfflineMessages: " + toJID + " > " + message);
     final Message newMessage = new Message(toJID, Message.Type.chat);
     newMessage.setBody(message);
     DelayInformation delay = new DelayInformation(new Date(ts));
     newMessage.addExtension(delay);
     newMessage.addExtension(new DelayInfo(delay));
     newMessage.addExtension(new DeliveryReceiptRequest());
     if ((packetID != null) && (packetID.length() > 0)) {
       newMessage.setPacketID(packetID);
     } else {
       packetID = newMessage.getPacketID();
       mark_sent.put(ChatConstants.PACKET_ID, packetID);
     }
     Uri rowuri =
         Uri.parse(
             "content://" + ChatProvider.AUTHORITY + "/" + ChatProvider.TABLE_NAME + "/" + _id);
     mContentResolver.update(rowuri, mark_sent, null, null);
     mXMPPConnection.sendPacket(
         newMessage); // must be after marking delivered, otherwise it may override the
     // SendFailListener
   }
   cursor.close();
 }
  /**
   * Helper function used to send a message to a contact, with the given extensions attached.
   *
   * @param to The contact to send the message to.
   * @param toResource The resource to send the message to or null if no resource has been specified
   * @param message The message to send.
   * @param extensions The XMPP extensions that should be attached to the message before sending.
   * @return The MessageDeliveryEvent that resulted after attempting to send this message, so the
   *     calling function can modify it if needed.
   */
  private MessageDeliveredEvent sendMessage(
      Contact to, ContactResource toResource, Message message, PacketExtension[] extensions) {
    if (!(to instanceof ContactJabberImpl))
      throw new IllegalArgumentException("The specified contact is not a Jabber contact." + to);

    assertConnected();

    org.jivesoftware.smack.packet.Message msg = new org.jivesoftware.smack.packet.Message();

    String toJID = null;

    if (toResource != null) {
      if (toResource.equals(ContactResource.BASE_RESOURCE)) {
        toJID = to.getAddress();
      } else toJID = ((ContactResourceJabberImpl) toResource).getFullJid();
    }

    if (toJID == null) {
      toJID = to.getAddress();
    }

    msg.setPacketID(message.getMessageUID());
    msg.setTo(toJID);

    for (PacketExtension ext : extensions) {
      msg.addExtension(ext);
    }

    if (logger.isTraceEnabled())
      logger.trace("Will send a message to:" + toJID + " chat.jid=" + toJID);

    MessageDeliveredEvent msgDeliveryPendingEvt =
        new MessageDeliveredEvent(message, to, toResource);

    MessageDeliveredEvent[] transformedEvents =
        messageDeliveryPendingTransform(msgDeliveryPendingEvt);

    if (transformedEvents == null || transformedEvents.length == 0) return null;

    for (MessageDeliveredEvent event : transformedEvents) {
      String content = event.getSourceMessage().getContent();

      if (message.getContentType().equals(HTML_MIME_TYPE)) {
        msg.setBody(Html2Text.extractText(content));

        // Check if the other user supports XHTML messages
        // make sure we use our discovery manager as it caches calls
        if (jabberProvider.isFeatureListSupported(toJID, HTML_NAMESPACE)) {
          // Add the XHTML text to the message
          XHTMLManager.addBody(msg, OPEN_BODY_TAG + content + CLOSE_BODY_TAG);
        }
      } else {
        // this is plain text so keep it as it is.
        msg.setBody(content);
      }

      // msg.addExtension(new Version());

      if (event.isMessageEncrypted() && isCarbonEnabled) {
        msg.addExtension(new CarbonPacketExtension.PrivateExtension());
      }

      MessageEventManager.addNotificationsRequests(msg, true, false, false, true);

      String threadID = getThreadIDForAddress(toJID);
      if (threadID == null) threadID = nextThreadID();

      msg.setThread(threadID);
      msg.setType(org.jivesoftware.smack.packet.Message.Type.chat);
      msg.setFrom(jabberProvider.getConnection().getUser());

      jabberProvider.getConnection().sendPacket(msg);

      putJidForAddress(toJID, threadID);
    }

    return new MessageDeliveredEvent(message, to, toResource);
  }
Пример #4
0
  /**
   * Parses a message packet.
   *
   * @param parser the XML parser, positioned at the start of a message packet.
   * @return a Message packet.
   * @throws Exception if an exception occurs while parsing the packet.
   */
  public static Packet parseMessage(XmlPullParser parser) throws Exception {
    Message message = new Message();
    String id = parser.getAttributeValue("", "id");
    message.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);
    message.setTo(parser.getAttributeValue("", "to"));
    message.setFrom(parser.getAttributeValue("", "from"));
    message.setType(Message.Type.fromString(parser.getAttributeValue("", "type")));
    String language = getLanguageAttribute(parser);

    // determine message's default language
    String defaultLanguage = null;
    if (language != null && !"".equals(language.trim())) {
      message.setLanguage(language);
      defaultLanguage = language;
    } else {
      defaultLanguage = Packet.getDefaultLanguage();
    }

    // Parse sub-elements. We include extra logic to make sure the values
    // are only read once. This is because it's possible for the names to appear
    // in arbitrary sub-elements.
    boolean done = false;
    String thread = null;
    Map<String, Object> properties = null;
    while (!done) {
      int eventType = parser.next();
      if (eventType == XmlPullParser.START_TAG) {
        String elementName = parser.getName();
        String namespace = parser.getNamespace();
        if (elementName.equals("subject")) {
          String xmlLang = getLanguageAttribute(parser);
          if (xmlLang == null) {
            xmlLang = defaultLanguage;
          }

          String subject = parseContent(parser);

          if (message.getSubject(xmlLang) == null) {
            message.addSubject(xmlLang, subject);
          }
        } else if (elementName.equals("body")) {
          String xmlLang = getLanguageAttribute(parser);
          if (xmlLang == null) {
            xmlLang = defaultLanguage;
          }

          String body = parseContent(parser);

          if (message.getBody(xmlLang) == null) {
            message.addBody(xmlLang, body);
          }
        } else if (elementName.equals("thread")) {
          if (thread == null) {
            thread = parser.nextText();
          }
        } else if (elementName.equals("error")) {
          message.setError(parseError(parser));
        } else if (elementName.equals("properties") && namespace.equals(PROPERTIES_NAMESPACE)) {
          properties = parseProperties(parser);
        }
        // Otherwise, it must be a packet extension.
        else {
          message.addExtension(
              PacketParserUtils.parsePacketExtension(elementName, namespace, parser));
        }
      } else if (eventType == XmlPullParser.END_TAG) {
        if (parser.getName().equals("message")) {
          done = true;
        }
      }
    }

    message.setThread(thread);
    // Set packet properties.
    if (properties != null) {
      for (String name : properties.keySet()) {
        message.setProperty(name, properties.get(name));
      }
    }
    return message;
  }