Beispiel #1
0
 /** 更改用户状态 */
 public void setPresence(int code) {
   if (connection == null) return;
   Presence presence;
   switch (code) {
     case 0:
       presence = new Presence(Presence.Type.available);
       connection.sendPacket(presence);
       Log.v("state", "设置在线");
       break;
     case 1:
       presence = new Presence(Presence.Type.available);
       presence.setMode(Presence.Mode.chat);
       connection.sendPacket(presence);
       Log.v("state", "设置Q我吧");
       System.out.println(presence.toXML());
       break;
     case 2:
       presence = new Presence(Presence.Type.available);
       presence.setMode(Presence.Mode.dnd);
       connection.sendPacket(presence);
       Log.v("state", "设置忙碌");
       System.out.println(presence.toXML());
       break;
     case 3:
       presence = new Presence(Presence.Type.available);
       presence.setMode(Presence.Mode.away);
       connection.sendPacket(presence);
       Log.v("state", "设置离开");
       System.out.println(presence.toXML());
       break;
     case 4:
       Roster roster = connection.getRoster();
       Collection<RosterEntry> entries = roster.getEntries();
       for (RosterEntry entry : entries) {
         presence = new Presence(Presence.Type.unavailable);
         presence.setPacketID(Packet.ID_NOT_AVAILABLE);
         presence.setFrom(connection.getUser());
         presence.setTo(entry.getUser());
         connection.sendPacket(presence);
         System.out.println(presence.toXML());
       }
       // 向同一用户的其他客户端发送隐身状态
       presence = new Presence(Presence.Type.unavailable);
       presence.setPacketID(Packet.ID_NOT_AVAILABLE);
       presence.setFrom(connection.getUser());
       presence.setTo(StringUtils.parseBareAddress(connection.getUser()));
       connection.sendPacket(presence);
       Log.v("state", "设置隐身");
       break;
     case 5:
       presence = new Presence(Presence.Type.unavailable);
       connection.sendPacket(presence);
       Log.v("state", "设置离线");
       break;
     default:
       break;
   }
 }
  /**
   * Parses a presence packet.
   *
   * @param parser the XML parser, positioned at the start of a presence packet.
   * @return a Presence packet.
   * @throws Exception if an exception occurs while parsing the packet.
   */
  public static Presence parsePresence(XmlPullParser parser) throws Exception {
    Presence.Type type = Presence.Type.available;
    String typeString = parser.getAttributeValue("", "type");
    if (typeString != null && !typeString.equals("")) {
      try {
        type = Presence.Type.valueOf(typeString);
      } catch (IllegalArgumentException iae) {
        System.err.println("Found invalid presence type " + typeString);
      }
    }
    Presence presence = new Presence(type);
    presence.setTo(parser.getAttributeValue("", "to"));
    presence.setFrom(parser.getAttributeValue("", "from"));
    String id = parser.getAttributeValue("", "id");
    presence.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);

    String language = getLanguageAttribute(parser);
    if (language != null && !"".equals(language.trim())) {
      presence.setLanguage(language);
    }
    presence.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);

    // Parse sub-elements
    boolean done = false;
    while (!done) {
      int eventType = parser.next();
      if (eventType == XmlPullParser.START_TAG) {
        String elementName = parser.getName();
        String namespace = parser.getNamespace();
        if (elementName.equals("status")) {
          presence.setStatus(parser.nextText());
        } else if (elementName.equals("priority")) {
          try {
            int priority = Integer.parseInt(parser.nextText());
            presence.setPriority(priority);
          } catch (NumberFormatException nfe) {
            // Ignore.
          } catch (IllegalArgumentException iae) {
            // Presence priority is out of range so assume priority to be zero
            presence.setPriority(0);
          }
        } else if (elementName.equals("show")) {
          String modeText = parser.nextText();
          try {
            presence.setMode(Presence.Mode.valueOf(modeText));
          } catch (IllegalArgumentException iae) {
            System.err.println("Found invalid presence mode " + modeText);
          }
        } else if (elementName.equals("error")) {
          presence.setError(parseError(parser));
        } else if (elementName.equals("properties") && namespace.equals(PROPERTIES_NAMESPACE)) {
          Map<String, Object> properties = parseProperties(parser);
          // Set packet properties.
          for (String name : properties.keySet()) {
            presence.setProperty(name, properties.get(name));
          }
        }
        // Otherwise, it must be a packet extension.
        else {
          try {
            presence.addExtension(
                PacketParserUtils.parsePacketExtension(elementName, namespace, parser));
          } catch (Exception e) {
            System.err.println("Failed to parse extension packet in Presence packet.");
          }
        }
      } else if (eventType == XmlPullParser.END_TAG) {
        if (parser.getName().equals("presence")) {
          done = true;
        }
      }
    }
    return presence;
  }